我正在编写一个python脚本,它负责组织您的下载文件夹,将每种类型的文件安排在一个单独的文件夹中。我正在使用watchdog检查下载文件夹中的修改。如果有东西开始下载,脚本就会运行,尽管我想等到下载完成后再运行我的脚本。
我不知道如何使用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()
发布于 2020-03-18 23:20:09
我也遇到过类似的问题,这对我来说是可行的。等待文件传输完成后再进行处理:
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()
发布于 2020-03-16 01:53:36
我认为这不是更好的解决方案,但你可以每隔1分钟查看一次修改日期,如果相同,则认为文件已完成。
https://stackoverflow.com/questions/60695881
复制相似问题