Checking when shutil.copyfile is done
Asked Answered
S

2

10

I have a such code:

for file in file_list:
    shutil.copyfile(file,newpath)
#do further actions

And here is the question, at #do further actions I use the copied f iles thus I need to make sure the shutil.copyfile functions finish their task. How can I make sure of this ?

Spool answered 29/6, 2010 at 13:49 Comment(0)
I
5

The shutil functions should return only after the operation is finished. If nothing funny is going on on the OS level it should be safe this way.

If you know the size of the file you could check if that is correct.

Inharmonic answered 29/6, 2010 at 13:56 Comment(3)
Modern OSes do heavy caching on the filesystem level (in modern Linux distributions, it can take up to one minute until the data actually gets written), and the disk controllers do additional caching. Of course, the latter can't be controlled by applications.Staircase
But the correct (cached) data should be returned if an application requests the file. I was more thinking about NFS with the async option or something similar.Inharmonic
In my case I was running dependent subprocesses without .wait()ing for them to finish.Katey
C
3

copyfile is a blocking function. By the time you get to #do further actions it should always be done. Are you having issues where it isn't?

Chapeau answered 29/6, 2010 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.