34

I'm working on a test case for which I create some subdirs. However, I don't seem to have the permission to remove them anymore. My UA is an Administrator account (Windows XP).

I first tried:

folder="c:/temp/" 
for dir in os.listdir(folder): 
    os.remove(folder+dir)

and then

folder="c:/temp/" 
os.remove(folder+"New Folder")

because I'm sure "New Folder" is empty. However, in all cases I get:

Traceback (most recent call last): 
  File "<string>", line 3, in <module> 
WindowsError: [Error 5] Access is denied: 'c:/temp/New Folder'

Does anybody know what's going wrong?

Sergio Da Silva
  • 341
  • 1
  • 3
  • 3

13 Answers13

48

os.remove requires a file path, and raises OSError if path is a directory.

Try os.rmdir(folder+'New Folder')

Which will:

Remove (delete) the directory path. Only works when the directory is empty, otherwise, OSError is raised.

Making paths is also safer using os.path.join:

os.path.join("c:\\", "temp", "new folder")
Aesthete
  • 18,622
  • 6
  • 36
  • 45
41

try the inbuilt shutil module

shutil.rmtree(folder+"New Folder")

this recursively deletes a directory, even if it has contents.

appusajeev
  • 2,129
  • 3
  • 22
  • 20
22

For Python 3.6, the file permission mode should be 0o777:

os.chmod(filePath, 0o777)
os.remove(filePath)
Pang
  • 9,564
  • 146
  • 81
  • 122
13

os.remove() only works on files. It doesn't work on directories. According to the documentation:

os.remove(path) Remove (delete) the file path. If path is a directory, OSError is raised; see rmdir() below to remove a directory. This is identical to the unlink() function documented below. On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.

use os.removedirs() for directories

allcaps
  • 10,945
  • 1
  • 33
  • 54
8bitwide
  • 2,071
  • 1
  • 17
  • 24
  • 4
    os.rmdir() will remove an empty directory. shutil.rmtree() will delete a directory and all its contents. – sparrow Jul 29 '16 at 18:55
10

U can use Shutil module to delete the dir and its sub folders

import os
import shutil

for dir in os.listdir(folder):
    shutil.rmtree(os.path.join(folder,dir))
Rakesh
  • 81,458
  • 17
  • 76
  • 113
5

Use os.rmdir instead of os.remove to remove a folder

os.rmdir("d:\\test")

It will remove the test folder from d:\\ directory

e-frank
  • 739
  • 11
  • 21
arun
  • 51
  • 1
  • 2
4

If you want remove folder, you can use

os.rmdir(path)
Zoe
  • 27,060
  • 21
  • 118
  • 148
a w
  • 41
  • 1
1

If it's a directory, then just use:

os.rmdir("path")
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
sofblocks
  • 9
  • 2
1
import os
import shutil


dir = os.listdir(folder)
for file in dir:
    if os.path.isdir(f'{folder}\\{file}'):
       shutil.rmtree(os.path.join(f'{folder}\\{file}'))
    else:
       os.remove(f'{folder}\\{file}')
jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
  • While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply – jmoerdyk Apr 11 '23 at 17:07
  • folder= path of your file or folder, loop for to check all the items in the directory, and say if it is a file or folder, condition if to validate if it is a folder use shutil if it is a file use file – douglas carmaem Apr 11 '23 at 18:32
  • @douglas carmaem ... Please add the additional explanation directly in your question, not in a comment. Your additional explanation is still unclear. Please take the time to use complete sentences, proper punctuation and capitalization. This answer could stay here for many years to come. Thank you. – Rich Lysakowski PhD Apr 16 '23 at 04:26
0

File is in read only mode so change the file permission by os.chmod() function and then try with os.remove().

Ex:

Change the file Permission to 0777 and then remove the file.

os.chmod(filePath, 0777)
os.remove(filePath)
Vasfed
  • 18,013
  • 10
  • 47
  • 53
Pradeep S
  • 21
  • 2
  • 10
0

The reason you can't delete folders because to delete subfolder in C: drive ,you need admin privileges Either invoke admin privileges in python or do the following hack

Make a simple .bat file with following shell command

del /q "C:\Temp\*"

FOR /D %%p IN ("C:\temp\*.*") DO rmdir "%%p" /s /q

Save it as file.bat and call this bat file from your python file

Bat file will handle deleting subfolders from C: drive

Pang
  • 9,564
  • 146
  • 81
  • 122
0

Can't remove a folder with os.remove

import os

if os.path.exists("demofile.txt"):
  os.remove("demofile.txt")
else:
  print("The file does not exist")
Anton Krug
  • 1,555
  • 2
  • 19
  • 32
  • So your answer is testing if the file exists and if it doesn't then it doesn't try to remove it. However the OP was asking about the folder. – Anton Krug Jun 23 '21 at 18:21
0

In my case it was due to lack of admin privileges. I solved it running terminal or cmd as administrator

windows key -> cmd -> right click -> run as administrator