开发者

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 program开发者_开发问答matically 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.


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.


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.


If you know where the files are being copied from, you can check to see whether the size of the copy has reached the size of the original.

Alternatively, if a file's size doesn't change for a couple of seconds, it is probably done being copied, which may be good enough. (May not work well for slow network connections, however.)


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜