[英]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.