【问题标题】:How to check if the file is completely downloaded in a folder using python如何使用python检查文件是否完全下载到文件夹中
【发布时间】:2020-06-26 23:16:41
【问题描述】:

我正在编写一个 python 脚本,该脚本负责通过将每种类型的文件安排在一个单独的文件夹中来组织您的下载文件夹。我正在使用看门狗检查下载文件夹中的修改。如果有任何东西开始下载,脚本就会运行,尽管我想等到下载完成后再运行我的脚本。

我不知道如何检查下载文件是否完全使用 python 下载。

我已包含代码以显示我的脚本的基本工作原理。

class ShiftingFiles(FileSystemEventHandler):
"""
This class is going to allow us to override the FileSystemEventHandler
methods.
"""
# Overriding the on_modified() method of FileSystemEventHandler class
# in the watchdog API
def on_modified(self, event):
    self.shift()



if __name__ == "__main__":
    # To shift the files as soon as the program is run
    ShiftingFiles().shift()
    # Consuming watchdog API
    event_handler = ShiftingFiles()
    observer = Observer()
    observer.schedule(event_handler, download_location, recursive=False)
    observer.start()
    try:
        while True:
            time.sleep(1000)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

【问题讨论】:

标签: python python-3.x operating-system python-watchdog


【解决方案1】:

我遇到了类似的问题,这对我来说很有效。等待文件传输完成后再处理:

def on_modified(self, event):
        file_size = -1
        while file_size != os.path.getsize(event.src_path):
            file_size = os.path.getsize(event.src_path)
            time.sleep(1)

        self.shift()

【讨论】:

    【解决方案2】:

    我认为这不是更好的解决方案,但是您可以每隔 1 分钟看到最后修改日期,如果相同,则认为文件已完成。

    【讨论】:

    • 说得有道理,我试试看!
    • 我不确定如何使用它来确定文件是否已下载@Todd。我没有使用 python 下载任何东西。下载是使用浏览器完成的。我只是想看看它什么时候完全下载。
    猜你喜欢
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 2021-04-26
    • 2018-02-03
    • 2019-09-15
    相关资源
    最近更新 更多