这里有一个解决方案。未经测试!

请注意使用os.walk()就地更改dirnames的能力,告诉它不要递归到子目录中,并且避免使用{},这会破坏此功能:When topdown is True, the caller can modify the dirnames list in-place

(perhaps using del or slice assignment), and walk() will only recurse

into the subdirectories whose names remain in dirnames; this can be

used to prune the search, [...]

Modifying dirnames when topdown is False has no effect on the behavior of the walk, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated.

有关详细信息,请参见the docs。在

同样值得注意的是使用os.path.samefile()进行路径比较。这是available on Windows since version 3.2。在

这是未经测试的!

使用风险自负!!

说真的,小心点!!!import os

def gen_dir_paths_except(base_path, exceptions):

# behave like os.walk(): return nothing if the given path isn't a directory

if not os.path.isdir(base_path):

return

# keep only existing paths in exceptions, so that os.path.samefile() works

exceptions = list(filter(os.path.exists, exceptions))

# don't forget the base directory

if any(os.path.samefile(base_path, x) for x in exceptions):

return

yield base_path

for curr_dirpath, dirnames, filenames in os.walk(base_path):

# skip directories mentioned in exceptions

dirnames_to_skip = []

for dir_name in dirnames:

dir_path = os.path.join(curr_dirpath, dir_name)

if any(os.path.samefile(dir_path, x) for x in exceptions):

dirnames_to_skip.append(dir_name)

else:

yield dir_path

for dir_name in dirnames_to_skip:

dirnames.remove(dir_name)

def rmtree_except(path, exceptions):

# Check that the path is actually a directory. This is needed here

# because os.walk() will silently return nothing for a non-directory.

if not os.path.isdir(path):

if not os.path.exists(path):

raise OSError("No such directory: " + path)

else:

raise OSError("Not a directory: " + path)

# keep only existing paths in exceptions, so that os.path.samefile() works

exceptions = list(filter(os.path.exists, exceptions))

dirs = list(gen_dir_paths_except(path, exceptions))

# iterate through directories in bottom-up order

for dir_path in reversed(dirs):

filenames = [

x for x in os.listdir(dir_path)

if not os.path.isdir(os.path.join(dir_path, x))

]

for file_name in filenames:

# skip files mentioned in exceptions

file_path = os.path.join(dir_path, file_name)

if not any(os.path.samefile(file_path, x) for x in exceptions):

os.remove(file_path)

try:

os.rmdir(dir_path)

except OSError: # directory not empty

pass # just leave the directory

python 删除指定目录_删除Python中除一个子目录外的目录相关推荐

  1. js list删除指定元素_删除js数组中的指定元素,有这两步就够了

    js数组是js部分非常重要的知识,有时我们有这么个需求js数组删除指定元素,先定义一个函数来获取删除指定元素索引值,然后用js数组删除的方法,来删除指定元素即可,就两步不难,很简单. 1.JS的数组对 ...

  2. python按指定概率抽样_基于python进行抽样分布描述及实践详解

    本次选取泰坦尼克号的数据,利用python进行抽样分布描述及实践. 备注:数据集的原始数据是泰坦尼克号的数据,本次截取了其中的一部分数据进行学习.Age:年龄,指登船者的年龄.Fare:价格,指船票价 ...

  3. linux拷贝文件到多个目录,怎么在 Linux 中复制文件到多个目录中

    cp命令将文件复制到目录文件夹. 如果需要复制一个文件到多个文件夹下,可以使用多条cp语句,或者使用shell脚本. 用法示例: 复制abc.txt到ab cd ef目录下 $ cp abc.txt ...

  4. python删除指定行_关于csv:删除python中的特定行和对应文件

    我想删除90%的"转向"值等于0的行.这三个图像都有一个对应的图像文件,中间,左边和右边.我也要删除它们.csv文件如下: 我编写了以下代码,以至少获取转向值为0的文件.我所需要的 ...

  5. python删除txt指定内容_使用Python删除文本文件中的部分内容 | 学步园

    为了学习英语,我把从网上下载下来的电影转换成纯MP3文件,放到iTouch里去了,这样就可以直接练习听力了,另外把下载下来的字幕也放进去,听不懂的时候可以看,但有一个问题,网上载下来的字幕格式都如下所 ...

  6. python字符串去掉空行_从python中的字符串中删除空格

    python字符串去掉空行 如何在python中删除字符串中的空格 (How to remove whitespaces in a string in python) str.lstrip()str. ...

  7. java json删除指定元素_简洁而优雅,Python Tablib实现将数据导出为Excel, Json等N种格式...

    遇见 Tablib 我们在 Python 实际开发过程中,经常涉及将数据导出为 Excel.Csv.Yaml.Json 等各种格式的文件的需求,一些粗鲁的实现方式是通过安装各种第三方模块以支持不同格式 ...

  8. python 删除pdf页面_使用Python批量删除扫描PDF中的空白页

    对于经常看扫描PDF资料的人来说,经常会碰到如下问题: PDF缩略图 因为一些格式转换的原因,一些空白页时不时的出现,而且规律不定,一会是偶数页码一会是奇数页码,逐个选中删除的话,对于几百页的文档,非 ...

  9. python list去重并删除某些元素_使用Python实现list(列表)中的重复元素删除,例如: X= [1,1,2,a,a,[1,2,3]] 去重后:X= 「1,2,a,[1,2...

    题目要求的实质是列表内部元素的去重,有两种思路:第一种,删除的思路,判断列表中的元素是否出现重复,如果有重复,删除重复出现的元素直到剩下最后一个:第二种,添加的思路,新建空列表,将新列表中不包含.原列 ...

  10. java 删除指定字符_字符串删除指定位置字符 JAVA 删除字符串中指定的字符

    <死侍2>有多不按套路出牌? 要CSS布局HTML小编今天和大家分享用到函数的调用. 编制函数fun,其功能是:删除一个字符串中指定的一.问题描述:从键盘输入一个字符串给str和一个字符给 ...

最新文章

  1. 从乘法表JAVA意思4_四、Java从头开始-我的九九乘法表(二)
  2. LVM逻辑卷容量的增减
  3. 山东专升本access知识点_专升本计算机速背知识点(十八)
  4. 关于库存 库存BAPI
  5. oracle sql的正则表达式,Oracle SQL 语句中正则表达式的应用
  6. div加border样式
  7. pip离线下载安装依赖包,及github包,及常用pip源
  8. Python 机器学习经典实例
  9. (五十七)方差分析与相关分析
  10. 流量不清零跑得快风波,运营商到底在掩盖什么
  11. openGauss社区理事会正式成立!云和恩墨与3大运营商、7大头部银行等18家理事单位加入,共建、共享、共治优质社区...
  12. 春生冬至时——今日冬至
  13. 远程连接linux工具mob,ssh远程登录工具 mob,MobXterm与FileZilla
  14. 计算机0基础知识,计算机基础知识0课件.ppt
  15. WordPress优化教程大全
  16. Google map获取手机屏幕当前显示地图的范围
  17. 解决Windows电脑自带的画图工具无法打开png文件的问题
  18. dubbo服务注册ip地址不正确,rpc服务调用失败?
  19. php验证码大全(实例分享),php文件上传代码大全(实例分...-php验证码大全(实例分享)-php打印倒三角的实例代码_169IT.COM...
  20. linphone - Network is unreachable (真的时网络不可达)

热门文章

  1. PyQt5 基本语法(一):基类控件
  2. JavaCC详解 绝对干货
  3. Docker个人理解与初级使用
  4. python字符串加减乘除_从字符串解析加减乘除符号
  5. S3C2440c语言汇编传参点灯
  6. redis 错误 Error reply to PING from master: '-DENIED Redis is running in protected mode because prote
  7. 西方文化系列讲座之希腊文化(下)
  8. unity 物体移动方式的一些笔记
  9. android内核编译 me525,今天给ME525+刷了Android 4.0系统,很流畅!
  10. ue4创建c++类编译失败