我正在使用 Python 开发一个执行以下操作的应用程序:
- 监视特定目录并监视文件 转移给它。文件传输完成后,运行一些 文件上的外部程序。
我开发这个应用程序的主要问题是知道文件何时完成传输。据我所知,该文件将通过 SFTP 传输到特定目录。 Python 如何知道文件何时完成传输?我知道我可以使用 os.stat(fileName)
方法返回的对象中的 st_size
属性。我需要使用更多工具来实现这些目标吗?
最佳答案
我最终使用了看门狗的组合,一直等到我可以打开文件进行写入
#If there is no error when trying to read the file, then it has completely loaded
try:
with io.FileIO(fileName, "r+") as fileObj:
'''
Deal with case where FTP client uses extensions such as ".part" and '.filepart" for part of the incomplete downloaded file.
To do this, make sure file exists before adding it to list of completedFiles.
'''
if(os.path.isfile(fileName)):
completedFiles.append(fileName)
print "File=" + fileName + " has completely loaded."
except IOError as ioe:
print str(ioe)
关于python - 使用 Python 了解何时从 FTP 源完全接收到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18500496/