Can't remove a folder with os.remove (WindowsError: [Error 5] Access is denied: 'c:/temp/New Folder')

独自空忆成欢 提交于 2019-12-18 04:42:17

问题


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?


回答1:


try the inbuilt shutil module

shutil.rmtree(folder+"New Folder")

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




回答2:


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")



回答3:


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




回答4:


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))



回答5:


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

os.chmod(filePath, 0o777)
os.remove(filePath)



回答6:


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)



回答7:


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




回答8:


If you want remove folder, you can use

os.rmdir(path)



回答9:


If it's a directory, then just use:

os.rmdir("path")


来源:https://stackoverflow.com/questions/11625062/cant-remove-a-folder-with-os-remove-windowserror-error-5-access-is-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!