1

I am using a watchdog module to move video files to a different folder. But the problem is the move process starts as soon as the file is created and is still under copy process from its origin location. To overcome this, I did a file size check, but windows is not refreshing the filesize in real time and hence the file size check is not proving to be useful.

Is there another way to check if the file is fully copied (maybe if file can be read/opened?) prior to moving the file. I tried the code below but to no avail.

while True:
    try:
        with open(os.path.basename(event.src_path)) as f:
            s = f.read()
            print ("can read the file now")
    except IOError:
        print ("cannot read the file")
#shutil.move operation here

Any ideas will be appreciated.

srt243
  • 63
  • 6
  • Use `os.access(path, os.R_OK)`, from https://stackoverflow.com/a/8876254/13636407 – paime Jul 05 '20 at 09:48
  • 1
    @paime pretty sure that would not work as you still would be able to read a file that is being copied. That does not mean that the file copy has been completed. – rdas Jul 05 '20 at 09:50
  • Check last time it was modified. Compare with current time. If they differ by a minutes or so. Move it. It may work. last_modified = os.stat('filename').st_mtime. – Pramote Kuacharoen Jul 05 '20 at 09:57
  • yes, that didn't work as the file was instantly moved and then printed the `else` statement. I need to keep checking until the file has finished copying and _then_ move it. – srt243 Jul 05 '20 at 10:00
  • I don't understand. You check last time it was modified. If it is not over a minutes or so, you don't move it. How can the file be moved instantly? – Pramote Kuacharoen Jul 05 '20 at 10:08
  • @PramoteKuacharoen sorry, my response was to paime's suggestion. However, check last time will not work as I mentioned the filesize refresh is not in real time and hence the time might not modify for 10-15 minutes even and again an incomplete file will be moved. – srt243 Jul 05 '20 at 10:11
  • Does it takes that much time to refresh file status? – Pramote Kuacharoen Jul 05 '20 at 10:15
  • yes, because at times there are multiple ongoing file transfers and I have seen few files sitting at 1KB file size and then gradually growing. Not sure if it's got to do with windoes/ntfs. – srt243 Jul 05 '20 at 10:17
  • Another solution is to change the process of creating the file if you can control. You can use a different extension and rename it when it is finished. – Pramote Kuacharoen Jul 05 '20 at 10:17
  • yes, thought about that. Like implementing a .tmp or .lck extension but the software that creates these files does not have the functionality to do that. – srt243 Jul 05 '20 at 10:18

0 Answers0