Python 删除目录中特定文件

痞子三分冷 提交于 2019-11-27 01:23:23

代码如下,使用了递归:

 1 import sys
 2 currDir = sys.path[0]
 3 
 4 import os
 5 def removeFile(dir,postfix):
 6     if os.path.isdir(dir):
 7         for file in os.listdir(dir):
 8             removeFile(dir+'/'+file,postfix)
 9     else:
10         if os.path.splitext(dir)[1] == postfix:
11             os.remove(dir)
12 removeFile(currDir,'.txt')

 

转载于:https://www.cnblogs.com/nzbbody/p/3443811.html

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