I’m using stat to check if a file is still being transferred, but it’s not working. If I copy a large file over the network (talking hundreds of gigabytes, they can take several minutes) and I run the script
from os import stat
from time import sleep
While True:
stat("thefile.foo")
sleep(2)
I expect to see either the time or the size changing, but it stays the same. Is there anything else I can use to check if a file is still transferring? This is on a windows server, unfortunately.

I tried this script, to actually read the file:
import sys
finished=False
oldgb=0
while not finished:
f=open(thefile,"rb")
samp=f.read(1)
gb=0
while samp!= b'':
sys.stdout.flush()
gb+=1000000000
f.seek(gb)
samp=f.read(1)
print(gb/1000000000,samp)
f.close()
print(oldgb, gb)
if gb>oldgb:
oldgb=gb
else:
finished=True
And noticed that while the file was being copied the bytes that hadn’t been copied yet were initialised to zero, hence the file size not updating. I might use a checksum to check if the file has changed instead.
Ok here’s the hackalicious solution: first I installed sysinternals handle.exe. I use that to see if the files have any open handles, thusly:
edit: this works for files being transferred on the machine itself – eg copied from one local file to another, however no file handles are created if a file is being transferred by an external machine. However the mtime of the file is changed in that case (fortunately).
So for my use – in a script to scan a folder and check for new files – I ended up using a combination of checking for handles (for local files) and checking for mtime and size (for external files).