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?
To work around this issue, use either of the following methods: When you delete the files or folders by using Windows Explorer, use the SHIFT+DELETE key combination. This bypasses the Recycle Bin. Open a command prompt window and then use the rd /s /q command to delete the files or folders.
Syntax : Type RMDIR /S /Q “” (where is the Location of folder you wish to delete).
PermissionError: [errno 13] Permission denied occurs when someone tries to access a file from Python without having the necessary permissions. To fix this error, use the full chmod or chown command to change the permissions on the file to allow the correct user and/or squad to access the file.
Shutil rmtree() to Delete Non-Empty Directory The rmtree('path') deletes an entire directory tree (including subdirectories under it). The path must point to a directory (but not a symbolic link to a directory). Set ignore_errors to True if you want to ignore the errors resulting from failed removal.
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")
try the inbuilt shutil module
shutil.rmtree(folder+"New Folder")
this recursively deletes a directory, even if it has contents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With