使用Python知道何时从FTP源完全接收文件

Fea*_*ure 5 python ftp

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

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

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

Fea*_*ure 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)
Run Code Online (Sandbox Code Playgroud)