Monday, 30 November 2020

Wait for Windows file I/O to complete in Python

I have a set of system tests which fire up some processes, create files etc., then shut them all down and delete the files.

I am encountering two intermittent errors on the cleanup:

On a log file created by one of the processes:

    os.remove(log_path)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: <path_to_file>

When trying to delete the output directory with shutil.rmtree:

File "C:\Python27\lib\shutil.py", line 254, in rmtree
    os.rmdir(path)
WindowsError: [Error 145] The directory is not empty: 'C:\\TestTarget\\xxx'

Both errors go away if I insert a 2 second delay before the tidyup, so I think the problem is with the time Windows takes to release the files. Obviously I'd like to avoid putting in delays in my tests, is there a way to wait until the filesystem has caught up?



from Wait for Windows file I/O to complete in Python

No comments:

Post a Comment