5

我正在使用 Python 开发执行以下操作的应用程序:

  • 监视特定目录并监视要传输到它的文件。文件完成传输后,在文件上运行一些外部程序。

我开发此应用程序的主要问题是知道文件何时完成传输。据我所知,该文件将通过 SFTP 传输到特定目录。Python 如何知道文件何时完成传输?我知道我可以使用方法st_size返回的对象的属性os.stat(fileName)。我需要使用更多工具来实现这些目标吗?

4

2 回答 2

6

我最终使用了看门狗的组合并等到我可以打开文件进行写入

 #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)
于 2013-09-11T14:33:16.603 回答
4

解决这个问题的最好方法是让发送进程 SFTP 到一个保留区域,然后(可能使用 SSH)执行mv命令将文件从保留区域移动到最终目标区域。然后,一旦文件出现在目标区域,您的脚本就知道它已完全传输。

于 2013-08-29T00:38:43.700 回答