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