python shutil.move getting .filepart on my filename
Question:
im upload a file to api with the following code
files=[
('nzb_file',(nzbTitle,open(event.src_path,'rb'),'application/octet-stream'))
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
if response = 200 I want to move the file to DIR succes
if response contains something else. I want the file moved to another DIR
my problems is, Then I use shutil.move(xUpFile, nzbUpload) i get this error
FileNotFoundError: [Errno 2] No such file or directory: '/home/henrik/old_bot/Nzb/xxxxxxx.nzb.filepart'
I get this at the end of my file
.filepart
why and how do i solve this
I don’t now if it’s because the file still is open
Answers:
Requests doesn’t close the file for you, so you’ll need to call close() yourself, which means saving a reference to the file object. The following should work in Python 3.8+
files=[
('nzb_file',(nzbTitle,f:=open(event.src_path,'rb'),'application/octet-stream'))
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
f.close()
In versions before 3.8, you need to save off the file object reference before creating files:
f = open(event.src_path,'rb')
files=[
('nzb_file',(nzbTitle,f,'application/octet-stream'))
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
f.close()
In either version, using a with
block could also work since you’re only sending a single file, and the context manager will close the file for you. However this will also automatically catch any exceptions that are thrown within the block so in some cases you might not want that option.
with open(event.src_path,'rb') as f:
files=[
('nzb_file',(nzbTitle,f,'application/octet-stream'))
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
Also, unless you were specifically uploading a file with the .filepart
extension, that is a temporary file created as part of the FTP process which should automatically get renamed or deleted by the FTP process when the transfer is successfully completed, so the problem may be that you’re trying to move the temporary file which the system has already cleaned up.
im upload a file to api with the following code
files=[
('nzb_file',(nzbTitle,open(event.src_path,'rb'),'application/octet-stream'))
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
if response = 200 I want to move the file to DIR succes
if response contains something else. I want the file moved to another DIR
my problems is, Then I use shutil.move(xUpFile, nzbUpload) i get this error
FileNotFoundError: [Errno 2] No such file or directory: '/home/henrik/old_bot/Nzb/xxxxxxx.nzb.filepart'
I get this at the end of my file
.filepart
why and how do i solve this
I don’t now if it’s because the file still is open
Requests doesn’t close the file for you, so you’ll need to call close() yourself, which means saving a reference to the file object. The following should work in Python 3.8+
files=[
('nzb_file',(nzbTitle,f:=open(event.src_path,'rb'),'application/octet-stream'))
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
f.close()
In versions before 3.8, you need to save off the file object reference before creating files:
f = open(event.src_path,'rb')
files=[
('nzb_file',(nzbTitle,f,'application/octet-stream'))
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
f.close()
In either version, using a with
block could also work since you’re only sending a single file, and the context manager will close the file for you. However this will also automatically catch any exceptions that are thrown within the block so in some cases you might not want that option.
with open(event.src_path,'rb') as f:
files=[
('nzb_file',(nzbTitle,f,'application/octet-stream'))
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
Also, unless you were specifically uploading a file with the .filepart
extension, that is a temporary file created as part of the FTP process which should automatically get renamed or deleted by the FTP process when the transfer is successfully completed, so the problem may be that you’re trying to move the temporary file which the system has already cleaned up.