Reputation: 12007
I have a Python
function that acts as a "listener" for a file to be added to a folder (from an external process).
I am currently doing this using an infinite loop where I check for the file size to stop increasing (signifying that the file has finished being placed in the folder, and the rest of the python function can go on to open the file).
file_size = 0
while True:
file_info = os.stat(file_path)
if file_info.st_size == 0 or file_info.st_size > file_size:
file_size = file_info.st_size
sleep(1)
else:
break
This works, but I know its a hack. Polling an increasing file_size every one second doesn't seen like the best way to do this.
Is there another way to use Python
to determine if an file copy (from an external process) has completed?
Upvotes: 2
Views: 5921
Reputation: 3588
You should rely on the kernel to notify your program when a file or directory has changed.
On Linux, you can take advantage of the inotify system call using: https://github.com/seb-m/pyinotify or similar libraries. From the pyinotify
examples:
import pyinotify
# Instanciate a new WatchManager (will be used to store watches).
wm = pyinotify.WatchManager()
# Associate this WatchManager with a Notifier (will be used to report and
# process events).
notifier = pyinotify.Notifier(wm)
# Add a new watch on /tmp for ALL_EVENTS.
wm.add_watch('/tmp', pyinotify.ALL_EVENTS)
# Loop forever and handle events.
notifier.loop()
Upvotes: 3