【发布时间】:2014-02-18 14:12:54
【问题描述】:
我正在使用 Python 的多个会话同时从 ftp 服务器下载多个文件。在某些时候,一个会话(我怀疑)读取了另一个进程正在访问的文件并引发以下错误:
Traceback (most recent call last): File
"F:\utilities\python\downloadFTP_NV.py", line 66, in <module>
os.unlink(FILE) WindowsError: [Error 32] The process cannot access the file because it is being used by another process:
u'm_4011851_ne_11_1_20100620.tif'
这是我似乎无法找到处理错误并移至下一个要下载的文件的最佳方法的代码块:
# Set logic so that already downloaded or partially
# downloaded files will not be downloaded again
if os.path.exists(fileCheck): # "fileCheck" is a file prior to renaming
print 'File "%s" exists already' % name
pass
elif os.path.exists(fileCheck2): # "fileCheck2 is a file after renaming
print 'File "%s" exists already' % FILE
pass
else:
try:
f.cwd(DIRN + folder)
start = time.clock()
f.retrbinary('RETR %s' % FILE, open(FILE, 'wb').write)
end = time.clock()
arcpy.Rename_management(os.path.join(workspace, FILE), os.path.join(workspace, name))
except ftplib.error_perm:
print 'ERROR: cannot read file "%s"' % FILE
os.unlink(FILE)
我考虑过添加另一个带有time.sleep(5) 的except 语句,以帮助减少重叠进程。或者可能只是删除os.unlink(FILE) 行。 处理此类错误的最佳方法是什么?
【问题讨论】:
标签: python error-handling ftp ftplib