2

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?

Brett
  • 11,637
  • 34
  • 127
  • 213
  • Please see if this can help: http://stackoverflow.com/questions/6825994/check-if-a-file-is-open-in-python – Hai Vu Dec 27 '13 at 21:12
  • Do you have control of this external process? Its common to copy a file to a temporary file and then rename it when complete to deal with this type of situation. – tdelaney Dec 27 '13 at 21:12
  • While a file is being written to, it's probably locked. Perhaps you could check its status (or lock it yourself) with the `poixfile` module. – martineau Dec 27 '13 at 21:14

1 Answers1

3

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()
gabrtv
  • 3,558
  • 2
  • 23
  • 28