我使用多个Python会话同时从一个ftp服务器下载多个文件。在某些时候,一个会话(我怀疑)读取正在由另一个进程访问的文件,并引发以下错误:处理并行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)
我曾经想过加入另一except
语句和time.sleep(5)
,以帮助减少重叠的过程。或者也许只是删除os.unlink(FILE)
行。 处理这类错误的最佳方法是什么?