Python 删除目录中特定文件


代码如下,使用了递归:

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM