Reputation: 21
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'
Upvotes: 2
Views: 1426
Reputation: 6143
This error occurred because of these reasons.
rmdir
use for to delete Empty directories. make sure your mentioned directory is empty. If it is true use shutil.rmtree(<directory>)
to remove directry with contents.import shutil
shutil.rmtree("<directory>")
C:/Python/Repositories/model-enveloper/Test/aux
instead of C:\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"))
Upvotes: 2