I'm using this code to upload myfile.txt from my windows machine to a ftp server. after the upoad the script deletes the file on my local machine (I'm not deleting it on the ftp).
try:
ftp = FTP(ftp.host.com)
ftp.login(your_username, your_password)
file = open(myfile.txt, "rb")
ftp.storbinary('STOR myfile.txt', file)
print 'STORing File now...'
ftp.quit()
file.close()
subprocess.Popen('del myfile.txt', shell=True)
print 'File deleted'
except all_errors:
print 'An error occured'
This code runs, however it's not reliable! At every ~10th upload my script hangs while STORing the file.
print 'STORing File now...' # So I just get 'STORING File now...'
The file is not big and should be uploaded within a few seconds, but I often have to wait an hour or two and only then the exception is thrown:
print 'An error occured'
If the exception were thrown 'earlier' it would be nice so I could just restart the upload (e.g. in a while loop). Because I need this file to be uploaded as soon as possible I need to make the file upload faster (I don't want to wait so long for the exception being thrown)
Second issue: Sometimes this happens: After the file has been successfully uploaded, the script fails to delete the file on my local machine because 'some other process is accessing it already' <- I think ftplib did not 'released' the file. What can I do to prevent this?
I'm searching for a better/reliable simple fileupload solution. Anyone have an idea? Thanks!