EDIT: Solution-
import os
import time
def main( event, path ):
if os.path.exists(path):
while True:
try:
new_path= path + "_"
os.rename(path,new_path)
os.rename(new_path,path)
time.sleep(0.05)
print("event type: %s path: %s " %(event.event_type, path))
break
except OSError:
time.sleep(0.05)
I utilized the fact that no two processes can simulatenously utilize a file for IO operations. In windows, when a file is copying, it is kept open by an OS process. Once copying is done, the file is closed by the OS process, and the os module in python can finally rename the file successfully
@Anmol- Not a duplicate. The issue with this code , in windows, is that the os reserves a binary file and writes over it while copying, and hence the size displayed by os.stat won't incrementally increase. We would want a piece of code that notifies us whenever image copying has completed. This particular code is a polling code which works well on linux, and notifies us when the copying process is done (in linux, the size increases incrementally with time)
import os
import time
def main( event,path ):
historicalSize = -1
while (historicalSize != os.path.getsize(path)):
historicalSize = os.stat(path).st_size
print("Size now %s" %historicalSize)
time.sleep(1)
else: print( "event type: %s path: %s " %(event.event_type, path))
Output should be like
Size now 124
Size now 12345
Size now 238590
.....
, instead of just
Size now 23459066950