简体   繁体   English

ftplib与python中的os.unlink结合使用

[英]ftplib in combination with os.unlink in python

With the following code I upload file.txt to a ftp server. 使用以下代码我将file.txt上传到ftp服务器。 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: 代码有效,但有时(每次〜第10次上传)我收到此错误消息:

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"! 但是使用“try except block”我也会得到相同的错误“进程无法访问该文件,因为它正被另一个进程使用”! 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? 我怎样才能让os.unlink正确地完成他的工作? Thanks! 谢谢!

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

您需要在try / except的except脚上关闭(文件和ftp会话),否则文件将被“旧”超时ftp会话引用(因此您需要在此while内打开文件)循环,而不是在它之外) - 关闭文件和在else分支上的ftp会话是不够的,因为它没有摆脱失败的,超时尝试(如果有的话)的引用。

I have still problems with the code, it is not as robust as I need it to be. 我仍然遇到代码问题,它不像我需要的那样强大。 For example it is possible that the login process fails. 例如,登录过程可能会失败。 Maybe user+pass is wrong, maybe the sesrver is busy. 也许用户+传递错误,也许sesrver很忙。

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 the ftp.quit() in the except block. 问题是except块中的ftp.quit()。 Python returns the following error (NOT exception): Python返回以下错误(非异常):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM