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

问题描述:

使用以下代码将file.txt上载到ftp服务器。当文件上传时,我在本地机器上删除它。ftplib与python中的os.unlink结合使用

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 

代码工作,但有时(每个〜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' 

所以文件不能使用,因为“它没有被释放”或东西删除了?我也试图断开链接的文件是这样的:

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) 

但与“尝试except块”我也得到了同样的错误“的进程无法因为它正在usedby另一个进程访问文件”!该脚本甚至没有尝试打印该例外:

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

刚刚停止(如上所述)。

我该如何让os.unlink正确地完成他的工作? 谢谢!

+0

你确定该文件未在文本编辑器中打开吗?或者是开放的,文本编辑器仍然没有关闭? – SilentGhost 2010-01-17 14:36:05

+0

对不起,我忘了我在ftplib中使用了一个while循环!不知道它是否相关,但是在STORing之后,我得到了一个“超时”的异常,然后重新启动了ftpupload。之后,os.unlink不起作用(正在使用进程...) – creativz 2010-01-17 14:41:09

+0

'all_errors'究竟是什么?如果不存在“WindowsError”(也不是它的超类),那就是为什么异常不会被捕获。 – balpha 2010-01-17 14:42:48

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 

您需要关闭(文件和ftp会话)的except腿试试/除,否则文件一直由“旧”超时ftp会话(所以你需要一个后果引用在while循环内打开该文件,而不是在其外部) - 仅仅关闭else支路上的文件和ftp会话是不够的,因为它不能摆脱失败,超时尝试(如果有的话)的引用, 。

+0

可以把我的整个ftpupload代码放在一个新的def和os.unlink之外的def(甚至在另一个def)中吗?这是否解决了ftp会话问题? – creativz 2010-01-17 18:57:37

+0

@creativz,只要确保文件和ftp会话无条件关闭(包括超时发生时),无论它们是处于相同的函数还是另一个函数,都将会很好。 – 2010-01-18 00:41:01

我仍然有问题的代码,它不像我所需要的那样健壮。 例如,登录过程可能失败。也许user + pass是错的,也许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) 

问题是在except块中的ftp.quit()。 Python返回以下错误(不例外):

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