Python文件操作--列出文件夾中py文件路徑


 1 import os
 2 
 3 pylist = []               #儲存指定文件夾中的所有文件路徑
 4 def print_directory(sPath):                 
 5     for sChild in os.listdir(sPath):
 6         sChildPath = os.path.join(sPath,sChild)
 7         if os.path.isdir(sChildPath):
 8             print_directory(sChildPath)
 9         else:     
10              pylist.append(sChildPath)
11        
12 z = os.path.abspath('.')   #當前文件夾中
13 y = os.path.join(z,'tu')    #當前文件夾中的‘tu’文件夾下
14 print_directory(y)
15 
16 p = [x for x in pylist if os.path.isfile(x) and os.path.splitext(x)[1]=='.py']
17 print p

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM