'check if a file is 'complete' (with python)

is it possible to check if a file is done copying of if its complete using python?
or even on the command line.
i manipulate files programmatically in a specific folder on mac osx but i need to check if the file is complete before running the code which makes the manipulation.



Solution 1:[1]

There's no notion of "file completeness" in the Unix/Mac OS X filesystem. You could either try locking the file with flock or, simpler, copy the files to a subdir of the destination directory temporarily, then move them out once they're fully copied (assuming you have control over the program that does the copying). Moving is an atomic operation; you'll know the file is completely copied once it appears at the expected path.

Solution 2:[2]

take the md5 of the file before you copy and then again whenever you think you are done copying. when they match you are good to go. use md5 from the hashlib module for this.

Solution 3:[3]

It seems like you have control of the (python?) program doing the copying. What commands are you using to copy? I would think writing your code such that it blocks until the copy operation is complete would be sufficient.

Is this program multi-threaded or processed? If so you could add file paths to a queue when they are complete and then have the other thread only act on items in the queue.

Solution 4:[4]

You can use lsof and parse the opened handle list. If some process still has an opened handle on the file (aka writing) you can find it there.

Solution 5:[5]

You can do this:

import os

# Get the file size two times, now and after 3 seconds.
size_1 = os.path.getsize(file_path)
time.sleep(3)
size_2 = os.path.getsize(file_path)
# compare the sizes.
if size_1 == size_2:
    # Do something.
else:
    # Do something else.

You can change the time to whatever suits your need.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fred Foo
Solution 2 sbartell
Solution 3 marr75
Solution 4 H. Stefan
Solution 5 ZSmain