With the following code I upload file.txt to a ftp server. When the file has been uploaded I delete it on my local machine.
import os
from ftplib import FTP
HOST = 'host.com'
FTP_NAME = 'username'
FTP_PASS = 'password'
filepath = 'C:\file.txt'
while True:
try:
ftp = FTP(HOST)
ftp.login(FTP_NAME, FTP_PASS)
file = open(filepath, 'r')
ftp.storlines('STOR file.txt', file)
ftp.quit()
file.close() # from this point on the file should not be in use anymore
print 'File uploaded, now deleting...'
except all_errors as e: #EDIT: Got exception here 'timed out'
print 'error' # then the upload restarted.
print str(e)
os.unlink(filepath) # now delete the file
The code works, but sometimes (every ~10th upload) I get this error message:
Traceback (most recent call last):
in os.unlink(filepath)
WindowsError: [Error 32] The process cannot access the file
because it is being usedby another process: 'C:\file.txt'
So the file cannot be deleted because 'it has not been released' or something? I also tried to unlink the file this way:
while True: # try to delete the file until it is deleted...
try:
os.unlink(filepath)
break
except all_errors as e:
print 'Cannot delete the File. Will try it again...'
print str(e)
But with the "try except block" I also get the same error "The process cannot access the file because it is being usedby another process"! The script didn't even try to print the exception:
'Cannot delete the File. Will try it again...'
and just stopped (like above).
How can I make os.unlink do his job properly? Thanks!