簡體   English   中英

使用Python知道何時從FTP源完全接收文件

[英]Using Python to Know When a File Has Completely Been Received From an FTP Source

我正在使用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)

解決此問題的最佳方法是將發送過程SFTP發送到保留區域,然后(可能使用SSH)執行mv命令將文件從保留區域移動到最終目標區域。 然后,一旦文件出現在目標區域中,您的腳本就會知道它已完全傳輸。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM