I have an issue with folders created through a Python script that cannot be removed.
I have a python script that runs other scripts using the 'runpy' module. The scripts will then create a folder using os.mkdir and a lot of matplotlib figures will be saved in there. When the script has run, and I try to delete the folder, I'm not allowed.
Through os.listdir the folder will show up:
In[5]: import os
'aux' in os.listdir(r'C:\Python\Repositories\model-enveloper\Test')
Out[5]: True
But trying to delete the folder with os.rmdir (not possible through windows explorer either):
In[6]: os.rmdir(r'C:\Python\Repositories\model-enveloper\Test\aux')
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-6-c835caa088bf>", line 1, in <module>
os.rmdir(r'C:\Python\Repositories\model-enveloper\Test\aux')
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Python\\Repositories\\model-enveloper\\Test\\aux'
1 Answer 1
This error occurred because of these reasons.
- You need to give admin permission to delete that directory.
rmdir
use for to delete Empty directories. make sure your mentioned directory is empty. If it is true useshutil.rmtree(<directory>)
to remove directry with contents.
import shutil
shutil.rmtree("<directory>")
- Try
C:/Python/Repositories/model-enveloper/Test/aux
instead ofC:\Python\Repositories\model-enveloper\Test\aux
@Bakuriu has mentioned it.
There for you can try to delete it like this
import os
os.rmdir(os.path.join("C:/Python/Repositories/model-enveloper/Test", "aux"))
-
1. I don't think it's an admin issue as I cannot delete it through explorer either and I have no problems with other folders. It seems that the folder is somehow defective (e.g. security information in explorer>properties are unavailable). 2. The folder is empty, but did also just try shutil.rmtree without any luck. 3. Tried many combinations "\" and "/".erenlef– erenlef2020年01月06日 13:43:43 +00:00Commented Jan 6, 2020 at 13:43
-
use only "/". by the way what is your current directry.Kalana– Kalana2020年01月06日 13:53:40 +00:00Commented Jan 6, 2020 at 13:53
-
Yes, I did try with only "/" - just like your last snip of code.erenlef– erenlef2020年01月06日 13:59:05 +00:00Commented Jan 6, 2020 at 13:59
-
How did you create aux directory.Kalana– Kalana2020年01月06日 14:05:13 +00:00Commented Jan 6, 2020 at 14:05
-
I have a python script that runs other scripts using the 'runpy' module. The scripts will then create a folder using os.mkdir and a lot of matplotlib figures will be saved in there.erenlef– erenlef2020年01月06日 14:08:46 +00:00Commented Jan 6, 2020 at 14:08
os.rmdir(r'C:/Python/Repositories/model-enveloper/Test/aux')
?rmdir
should raiseOSError
if the directory is not empty (see the docs).cmd
? just to make sure it's not anipython
issue