Python 遍歷一個目錄,輸出所有的文件名


python 獲取一個文件夾內(包括子文件夾)所有文件的名字和路徑

import os dir = "e:\\" for root, dirs, files in os.walk(dir): for file in files: print os.path.join(root,file)
  • 1
  • 2
  • 3
  • 4
  • 5

或:

import os path = r'e:\case' fns = [os.path.join(root,fn) for root, dirs, files in os.walk(path) for fn in files] for f in fns: print(f) print(len(fns))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
#coding=utf-8 import os def GetFileList(dir, fileList): newDir = dir if os.path.isfile(dir): fileList.append(dir.decode('gbk')) elif os.path.isdir(dir): for s in os.listdir(dir): #如果需要忽略某些文件夾,使用以下代碼 #if s == "xxx": #continue newDir=os.path.join(dir,s) GetFileList(newDir, fileList) return fileList list = GetFileList('D:\\workspace\\PyDemo\\fas', []) for e in list: print e


免責聲明!

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



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