Ftplib combined with os.unlink in python

In the following code, I upload the .txt file to the ftp server. When the file is 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 download) 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 else? I also tried to disable 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 "try except block" I also get the same error "The process cannot access the file because it is being used by another process"! The script did not even try to print the exception:

'Cannot delete the File. Will try it again...'

and just stopped (like above).

How can I get os.unlink to do my job correctly? Thank!

+3
source share
3
import os
from ftplib import FTP

HOST = 'host.com'
FTP_NAME = 'username'
FTP_PASS = 'password'
filepath = 'C:\file.txt'
file = open(filepath, 'r')
while True:
    try:
        ftp = FTP(HOST)
        ftp.login(FTP_NAME, FTP_PASS)        
        ftp.storlines('STOR file.txt', file)
    except all_errors as e: #EDIT: Got exception here 'timed out'
        print 'error'       #      then the upload restarted.
        print str(e)
    else:
        ftp.quit()
        file.close() # from this point on the file should not be in use anymore
        print 'File uploaded, now deleting...'   
        os.unlink(filepath) # now delete the file
        break
0

( ftp-) except try/except, "" - ftp- ( while, ) - ftp else, ( ).

0

I still have problems with the code, it is not as reliable as I need it. For example, it is possible that the login process failed. Maybe user + skip is wrong, maybe sesrver is busy.

try:
    ftp = FTP(HOST) # HOST is a valid host address
    ftp.login('test', 'test111111') # WRONG user + pass to test code robustness
    ftp.quit()
except all_errors as e:
    ftp.quit()
    print str(e)

The problem is ftp.quit () in the exception block. Python returns the following error (NOT an exception):

Traceback (most recent call last):
    File "test.py", line 9, in <module>
        ftp.quit()
NameError: name 'ftp' is not defined
0
source

Source: https://habr.com/ru/post/1728817/


All Articles