Reputation: 361
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?
Upvotes: 36
Views: 127334
Reputation: 1064
In case it helps - you could recursively delete files, and then directories, with try..except for each item, to ignore exceptions.
Realize this is not ideal, but in my case I tried many other things (using various delete functions, and a context manager to try to force release of file/directory locks) - but Python kept holding on to maybe 1 file out a huge set of files, preventing directory deletion.
Something like this will delete all files/directories that can be deleted, under 'temp_dir':
import os
import shutil
def _rmrf(temp_dir):
os.chmod(temp_dir, 0o777) # add full permissions
shutil.rmtree(temp_dir)
def _delete_files_recursively(temp_dir):
for dirpath, _dirnames, filenames in os.walk(temp_dir):
for f in filenames:
fp = os.path.join(dirpath, f)
try:
os.remove(fp)
except:
pass
def _delete_dirs_recursively(temp_dir):
for dirpath, dirnames, _filenames in os.walk(temp_dir):
for d in dirnames:
dp = os.path.join(dirpath, d)
try:
_rmrf(dp)
except:
pass
def delete_dir_contents(temp_dir):
_delete_files_recursively(temp_dir)
_delete_dirs_recursively(temp_dir)
A more complete example is here:
https://github.com/mrseanryan/py_utils/blob/master/util_robust_delete.py
Upvotes: 0
Reputation: 75
I has same issue on Server version OS. But that issue was not workstation type OS. I add next simple code (os.chmod) and an issue has been lost:
import os
import stat
os.chmod(file, stat.S_IWRITE)
os.remove(file)
Upvotes: 0
Reputation: 11
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}')
Upvotes: 1
Reputation: 49
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
Upvotes: 0
Reputation: 21
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")
Upvotes: 0
Reputation: 51
Use os.rmdir
instead of os.remove
to remove a folder
os.rmdir("d:\\test")
It will remove the test folder from d:\\
directory
Upvotes: 5
Reputation: 2071
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
Upvotes: 14
Reputation: 231
For Python 3.6, the file permission mode should be 0o777:
os.chmod(filePath, 0o777)
os.remove(filePath)
Upvotes: 23
Reputation: 1
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
Upvotes: 0
Reputation: 31
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)
Upvotes: 0
Reputation: 18850
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")
Upvotes: 48
Reputation: 82755
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))
Upvotes: 11
Reputation: 2299
try the inbuilt shutil module
shutil.rmtree(folder+"New Folder")
this recursively deletes a directory, even if it has contents.
Upvotes: 44