Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to copy a list of files on windows using python. When doing that manually, I see timeouts in some files, therefore the copy process fails. I need a way to implement the timeout check in python.

So far I use the win32 API:

import win32file
files = {'source_a' : 'dest_a', 'source_b' : 'dest_b'}

for f in files.keys():
    win32file.CopyFileW(f,files[f],0)

In some cases, the CopyFileW function doesn't return in "reasonable time", for the sake of this discussion, let's say 5 seconds. How can I wrap this function to be checked against a timer?

Edit: As suggested, I switched over to using the CopyFileEx function, because it has a cancellation Interface. If I put a timeout check in the callback function, the copy process is stalled as long as the callback is running. As I understand, the calls to the callback function only are issued, when there IS some file copying activity. If there ISN'T for a longer time, I don't see that.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
221 views
Welcome To Ask or Share your Answers For Others

1 Answer

Instead of CopyFile, use CopyFileEx which provides an interface that supports cancellation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...