使用Python了解完全从FTP源接收文件的时间 [英] Using Python to Know When a File Has Completely Been Received From an FTP Source

查看:185
本文介绍了使用Python了解完全从FTP源接收文件的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  • 监视特定目录并监视文件是否为
    转移给它。文件传输完成后,在该文件上运行一些
    的外部程序。



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

解决方案

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

 #如果试图读取文件时没有错误,那么它已经完全加载了
尝试:
与io.FileIO(fileName,r +)作为fileObj:

'''
处理FTP客户端使用诸如的扩展名的情况。部分和.filepart作为未完成下载文件的一部分
要做到这一点,在将文件添加到completedFiles列表之前,请确保文件存在
'''
if(os。 path.isfile(fileName)):
completedFiles.append(fileName)
printFile =+ fileName +已完全加载。
除IOError与ioe:
打印str(ioe)


I am using Python to develop an application that does the following:

  • Monitors a particular directory and watches for file to be transferred to it. Once the file has finished its transfer, run some external program on the file.

The main issue I have developing this application is knowing when the file has finished transferring. From what I know the file will be transferred via SFTP to a particular directory. How will Python know when the file has finished transferring? I know that I can use the st_size attribute from the object returned by os.stat(fileName) method. Are there more tools that I need to use to accomplish these goals?

解决方案

I ended up using a combination of watchdog and waited until I can open the file for writing

 #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了解完全从FTP源接收文件的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆