Reputation: 31
I am working on a python script that is responsible to organize your download folder, by arranging each type of file in a separate folder. I am using watchdog to check for modification in the download folder. If anything starts downloading the script runs although I want to wait till the download is complete before I run my script.
I cannot figure out how to check if downloading file is completely downloading using python.
I have included the code to show how my script basically works.
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()
Upvotes: 2
Views: 6152
Reputation: 582
I had a similar problem and this worked out for me. Wait that the transfer of the file is finished before processing it:
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()
Upvotes: 1
Reputation: 118
It isn't the better solution I think, but you can see last modification date at 1 minute interval, and if it's the same, consider that the file is completed.
Upvotes: 0