Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't delete test folder in Windows 7

Ihave a project setup like this

project/src/test/python/main.py
project/test-output

i want the main-testscript to delete and then recreate the test-output folder.

So i put this code there.

if os.path.exists("test-output"):
    shutil.rmtree("test-output")
os.mkdir("test-output")

But when i run it with

d:\projects\thisproject>python src\test\python\main.py

I get a access denies error from windows. [Error 5]

How to fix this?

the mkdir command works fine tough.

like image 268
KarlsFriend Avatar asked Jan 20 '26 12:01

KarlsFriend


1 Answers

I have recently seen this behaviour of shutil.rmtree() myself under Windows 7, also when creating and destroying test folders from unit tests. And, no, there was nothing having an active reference to the directory or anything in it, not even an explorer window. My 'solution' (if you can call it that) was to do:

shutil.rmtree("my/test/dir", ignore_errors=True)

Not exactly a thing of beauty, but it solved the problem for me.

By the way, this was a 64 bit Windows 7 machine, and on an SSD. A co-worker reported the same problem with shutil.rmtree() when deleting a directory on a Windows share on another machine. I'm getting a feeling that Windows is caching things here, if only briefly.

like image 142
grainednoise Avatar answered Jan 22 '26 02:01

grainednoise