如何使用python移除/删除非空文件夹?

摘要:
删除/删除非空文件夹/目录的最有效方法是什么?如果你想删除一个文件夹,无论它是否包含只读文件,请使用importshutil.rmtree2.从os上的python文档中。walk():#删除从目录中“top”可访问的所有内容,#假设有其他链接。#小心:这很危险!例如,iftop=='/',它#可以删除所有磁盘文件。importosforroot,dirs,filesinos。步行:fornameinfiles:os。removefornameindirs:os.rmdir3.从python 3.4中,您可以使用:importpathlibdefdelete_folder:forsubnpth.iterdir():ifsub。is_dir():删除_折叠:subunlink()pth。rmdir()#如果您只想删除内容,请删除此行,其中pth是路径库。路径实例。ImportosimportstatimportshutideferrorRemoveReadonly:excvalue=exc[1]金融指数值。错误号==错误号。EACES:#changethefiletobe可读、可写、可执行:0777os。chmod#retryfunnelse:#理由代码hereshutil。rmtree如果设置了忽略_错误,则忽略该错误;否则,如果设置了oneror,则调用它来处理带有参数的错误,其中func是os。listdir,操作系统。删除或操作。rmdir;路径是导致函数失败的函数的参数;exc_ Info是sys。exc_info()返回的元组。如果忽略错误为False,而OnError为None,则将引发异常。只需从python调用本机操作系统命令。

移除/删除非空文件夹/目录的最有效方法是什么?

1.标准库参考:shutil.rmtree。
根据设计,rmtree在包含只读文件的文件夹树上失败。如果要删除文件夹,不管它是否包含只读文件,请使用

import shutil
shutil.rmtree('/folder_name', ignore_errors=True)

2.从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))

3.从python 3.4可以使用:

import pathlib

def delete_folder(pth) :
    for sub in pth.iterdir() :
        if sub.is_dir() :
            delete_folder(sub)
        else :
            sub.unlink()
    pth.rmdir() # if you just want to delete dir content, remove this line

其中pth是pathlib.Path实例。很好,但可能不是最快的。

import os
import stat
import shutil

def errorRemoveReadonly(func, path, exc):
    excvalue = exc[1]
    if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
        # change the file to be readable,writable,executable: 0777
        os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)  
        # retry
        func(path)
    else:
        # raiseenter code here

shutil.rmtree(path, ignore_errors=False, onerror=errorRemoveReadonly)

如果设置了ignore_errors,则忽略错误;否则,如果设置了onerror,则调用它以使用参数(func、path、exc_info)处理错误,其中func是os.listdir、os.remove或os.rmdir;path是导致函数失败的函数的参数;exc_info是sys.exc_info()返回的元组。如果"忽略错误"为"假",而"OnError"为"无",则会引发异常。请在此处输入代码。

只需一些python3.5选项就可以完成上面的答案

删除空文件夹

import os
import shutil
from send2trash import send2trash # (shutil delete permanently)

root = r"C:UsersMeDesktop	est"  
for dir, subdirs, files in os.walk(root):  
    if subdirs == [] and files == []:
           send2trash(dir)
           print(dir,": folder removed")
   # 如果文件夹包含此文件,请同时删除它
   elif subdirs == [] and len(files) == 1: # if contains no sub folder and only 1 file
        if files[0]=="desktop.ini" or:  
            send2trash(dir)
            print(dir,": folder removed")
        else:
            print(dir)

    #删除仅包含.srt或.txt文件的文件夹
    elif subdirs == []: #if dir doesn’t contains subdirectory
        ext = (".srt",".txt")
        contains_other_ext=0
        for file in files:
            if not file.endswith(ext):  
                contains_other_ext=True
        if contains_other_ext== 0:
                send2trash(dir)
                print(dir,": dir deleted")
    

如果文件夹大小小于400KB,则删除该文件夹:

def get_tree_size(path):
   """Return total size of files in given path and subdirs."""
    total = 0
    for entry in os.scandir(path):
        if entry.is_dir(follow_symlinks=False):
            total += get_tree_size(entry.path)
        else:
            total += entry.stat(follow_symlinks=False).st_size
    return total


for dir, subdirs, files in os.walk(root):  
    If get_tree_size(dir) < 400000:  # ≈ 400kb
        send2trash(dir)
    print(dir,"dir deleted")

如果您确定要删除整个目录树,并且对目录的内容不再感兴趣,那么对整个目录树进行爬行是愚蠢的…只需从python调用本机操作系统命令即可。它将更快、更高效,而且内存消耗更少。

RMDIR c:lah /s /q

或* nix

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:] == '
': 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])

从docs.python.org:

This example shows how to remove a directory tree on Windows where
some of the files have their read-only bit set. It uses the onerror
callback to clear the readonly bit and reattempt the remove. 
import os, stat
import shutil

def remove_readonly(func, path, _):
   "Clear the readonly bit and reattempt the removal"
    os.chmod(path, stat.S_IWRITE)
    func(path)

shutil.rmtree(directory, onerror=remove_readonly)

在删除之前检查文件夹是否存在,这样更可靠。

import shutil
def remove_folder(path):
    # check if folder exists
    if os.path.exists(path):
         # remove if exists
         shutil.rmtree(path)
    else:
         # throw your exception to handle this special scenario
         raise XXError("your exception")
remove_folder("/folder_name")

如果您不想使用shutil模块,可以只使用os模块。

from os import listdir, rmdir, remove
for i in listdir(directoryToRemove):
    os.remove(os.path.join(directoryToRemove, i))
rmdir(directoryToRemove) # Now the directory is empty of files

def deleteDir(dirPath):
    deleteFiles = []
    deleteDirs = []
    for root, dirs, files in os.walk(dirPath):
        for f in files:
            deleteFiles.append(os.path.join(root, f))
        for d in dirs:
            deleteDirs.append(os.path.join(root, d))
    for f in deleteFiles:
        os.remove(f)
    for d in deleteDirs:
        os.rmdir(d)
    os.rmdir(dirPath)


为了简单起见,可以使用os.system命令:

import os
os.system("rm -rf dirname")

很明显,它实际上调用系统终端来完成这个任务。

删除一个文件夹,即使它可能不存在(避免了Charles Chow的答案中的竞争条件),但当其他事情出错时仍有错误(例如权限问题、磁盘读取错误、文件不是目录)
对于Python 3 .x:

import shutil

def ignore_absent_file(func, path, exc_inf):
    except_instance = exc_inf[1]
    if isinstance(except_instance, FileNotFoundError):
        return
    raise except_instance

shutil.rmtree(dir_to_delete, onerror=ignore_absent_file)

通过os.walk,我将提出由3个一行程序python调用组成的解决方案

python -c"import sys; import os; [os.chmod(os.path.join(rs,d), 0o777) for rs,ds,fs in os.walk(_path_) for d in ds]"
python -c"import sys; import os; [os.chmod(os.path.join(rs,f), 0o777) for rs,ds,fs in os.walk(_path_) for f in fs]"
python -c"import os; import shutil; shutil.rmtree(_path_, ignore_errors=False)"

第一个脚本chmod的所有子目录,第二个脚本chmod的所有文件。然后,第三个脚本会毫无障碍地删除所有内容。
我在Jenkins工作中的"shell脚本"中对此进行了测试(我不想将新的python脚本存储到SCM中,这就是为什么搜索单行解决方案),它适用于Linux和Windows。

使用python 3.7和linux仍然有不同的方法:

import subprocess
from pathlib import Path

#using pathlib.Path
path = Path('/path/to/your/dir')
subprocess.run(["rm","-rf", str(path)])

#using strings
path ="/path/to/your/dir"
subprocess.run(["rm","-rf", path])

本质上,它使用python的子进程模块来运行bash脚本$ rm -rf '/path/to/your/dir,就好像使用终端来完成相同的任务一样。它不是完全的python,但它可以完成。

我将pathlib.Path示例包括在内的原因是,根据我的经验,它在处理许多变化的路径时非常有用。导入pathlib.Path模块并将最终结果转换为字符串的额外步骤对于我的开发时间来说通常会降低成本。如果Path.rmdir()带有一个arg选项来显式处理非空的dir,那就方便了。

对于Windows,如果目录不是空的,并且您有只读文件,或者收到如下错误:

Access is denied
The process cannot access the file because it is being used by another process

试试这个,os.system('rmdir /S /Q"{}"'.format(directory))。
它相当于Linux/Mac中的rm -rf。

我找到了一种非常简单的方法来删除Windows操作系统上的任何文件夹(甚至不是空的)或文件。

os.system('powershell.exe  rmdir -r D:workspaceBranches*%s* -Force' %CANDIDATE_BRANCH)

参考:
https://www.codenong.com/303200/

免责声明:文章转载自《如何使用python移除/删除非空文件夹?》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇小目标 | DAX高级实践-Power BI与Excel联合应用python与selenium自动化基础-xlrd读取数据,Excel生成报告下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

【python】获取高德地图省市区县列表

项目中需要用省市区来进行检索,原想高德地图肯定会有API来获得这些数据,结果没有找到,有一个接口好像可以用,但是会附带大量的边界坐标点。 所以就不如自己把高德的省市区列表扒下来,自己写接口来完成这个功能。 看到高德地图的js的demo里面有这样的展示页面:http://lbs.amap.com/api/javascript-api/example/u/20...

python中使用multipart/form-data请求上传文件

最近测试的接口是上传文件的接口,上传单个文件,我主要使用了2种方法~ 接口例如: URL: http://www.baidu.com/*** method:post 参数: {"salary":19,"file":{}} 1、使用Python的requests上传表单数据和文件 data={"salary":salary} files={'file...

Python中的文件和目录操作实现代码

对于文件和目录的处理,虽然可以通过操作系统命令来完成,但是Python语言为了便于开发人员以编程的方式处理相关工作,提供了许多处理文件和目录的内置函数。重要的是,这些函数无论是在Unix、Windows还是Macintosh平台上,它们的使用方式是完全一致的。 本文将详细解释这些函数的使用方法。首先,我们介绍Python语言中类似于Windows...

docker应用、搭建、container、image、搭建私有云docker registry、容器通信、端口映射、多机多容器通信、数据持久化、docker部署wordpress、docker compose使用、负载均衡、docker Swarm、docker云部署

docker一、 容器技术和docker简介 1. 部署演变 l 在一台物理机部署Application l 虚拟化技术 2. 容器的必要性 l 开发人员开发一个Application需要各种环境,各种依赖 l 运维人员部署Application时也需要搭建各种环境 3. 容器解决的问题 l 解决了开发和运维之间的矛盾 4. 容器是什么 l 对软...

第一篇 pycharm安装及设置

1.下载pycharm包安装 (1)安装好Python 安装后在命令行中输入python 检查是否安装成功(进入py的交互行) (2)安装好Python 需要两个地方配置到环境变量里面 1.python的安装目录(python.exe在该文件夹中) 2.以及安装目录中的scripts(模块) 2.破解pycharm 3.pycharm的设置 (1)设置通关...

使用python操作zookeeper

kazoo 介绍 zookeeper的开发接口以前主要以java和c为主,随着python项目越来越多的使用zookeeper作为分布式集群实现,python的zookeeper接口也出现了很多,现在主流的纯python的zookeeper接口是kazoo。因此如何使用kazoo开发基于python的分布式程序是必须掌握的。 安装kazoo pip3 in...