1

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'
asked Jan 6, 2020 at 10:47
4
  • 2
    Did you try os.rmdir(r'C:/Python/Repositories/model-enveloper/Test/aux')? Commented Jan 6, 2020 at 10:52
  • 3
    the error says that the folder is not found, not that you're not allowed to delete it. Also, rmdir should raise OSError if the directory is not empty (see the docs). Commented Jan 6, 2020 at 10:54
  • The folder is empty and I did also try all sorts of combinations of "\" and "/". No luck. You are correct, the error is that it cannot find it - but it only cannot find it when I try to delete it. It does find the folder with os.listdir (and I can see it in explorer). Commented Jan 6, 2020 at 13:38
  • strange... did you try to run the command from cmd? just to make sure it's not an ipython issue Commented Jan 7, 2020 at 7:49

1 Answer 1

1

This error occurred because of these reasons.

  1. You need to give admin permission to delete that directory.
  2. 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>")
  1. Try 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"))
answered Jan 6, 2020 at 12:45
9
  • 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 "/". Commented Jan 6, 2020 at 13:43
  • use only "/". by the way what is your current directry. Commented Jan 6, 2020 at 13:53
  • Yes, I did try with only "/" - just like your last snip of code. Commented Jan 6, 2020 at 13:59
  • How did you create aux directory. Commented 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. Commented Jan 6, 2020 at 14:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.