当我试图删除一个非空文件夹时,我得到一个“访问被拒绝”错误。我在尝试中使用了以下命令:os.remove("/folder_name")。
删除一个非空文件夹/目录最有效的方法是什么?
当我试图删除一个非空文件夹时,我得到一个“访问被拒绝”错误。我在尝试中使用了以下命令:os.remove("/folder_name")。
删除一个非空文件夹/目录最有效的方法是什么?
当前回答
来自os.walk()的python文档:
# Delete everything reachable from the directory named in 'top',
# assuming there are no symbolic links.
# CAUTION: This is dangerous! For example, if top == '/', it
# could delete all your disk files.
import os
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
其他回答
你可以使用os。简单的系统命令:
import os
os.system("rm -rf dirname")
显然,它实际上调用系统终端来完成这项任务。
import shutil
shutil.rmtree(dest, ignore_errors=True)
我发现了一个非常简单的方法来删除任何文件夹(甚至不是空的)或文件在WINDOWS操作系统。
os.system('powershell.exe rmdir -r D:\workspace\Branches\*%s* -Force' %CANDIDATE_BRANCH)
我想添加一个“纯pathlib”方法:
from pathlib import Path
from typing import Union
def del_dir(target: Union[Path, str], only_if_empty: bool = False):
"""
Delete a given directory and its subdirectories.
:param target: The directory to delete
:param only_if_empty: Raise RuntimeError if any file is found in the tree
"""
target = Path(target).expanduser()
assert target.is_dir()
for p in sorted(target.glob('**/*'), reverse=True):
if not p.exists():
continue
p.chmod(0o666)
if p.is_dir():
p.rmdir()
else:
if only_if_empty:
raise RuntimeError(f'{p.parent} is not empty!')
p.unlink()
target.rmdir()
这依赖于Path是可排序的,长路径总是排在短路径之后,就像str一样。因此,目录将排在文件之前。如果我们反转排序,那么文件就会出现在它们各自的容器之前,所以我们可以简单地一次将它们逐个解除/rmdir链接。
好处:
它不依赖于外部二进制文件:所有东西都使用Python的电池包含的模块(Python >= 3.6) 这意味着它不需要重复启动一个新的子进程来解除链接 它非常快速和简单;你不必实现你自己的递归 它是跨平台的(至少,这是pathlib在Python 3.6中所承诺的;上述操作不能在Windows上运行) 如果需要,可以进行非常细粒度的日志记录,例如,记录每一次删除。
如果你确定你想要删除整个目录树,并且不再对目录的内容感兴趣,那么爬行整个目录树是愚蠢的…只需从python中调用本机操作系统命令即可。它将更快,更有效,更少的内存消耗。
RMDIR c:\blah /s /q
例如*那晚尼克斯。
rm -rf /home/whatever
在python中,代码看起来像..
import sys
import os
mswindows = (sys.platform == "win32")
def getstatusoutput(cmd):
"""Return (status, output) of executing cmd in a shell."""
if not mswindows:
return commands.getstatusoutput(cmd)
pipe = os.popen(cmd + ' 2>&1', 'r')
text = pipe.read()
sts = pipe.close()
if sts is None: sts = 0
if text[-1:] == '\n': text = text[:-1]
return sts, text
def deleteDir(path):
"""deletes the path entirely"""
if mswindows:
cmd = "RMDIR "+ path +" /s /q"
else:
cmd = "rm -rf "+path
result = getstatusoutput(cmd)
if(result[0]!=0):
raise RuntimeError(result[1])