码迷,mamicode.com
首页 > 编程语言 > 详细

python 遍历文件夹并统计文件数量

时间:2020-03-04 19:28:14      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:wal   name   pre   pytho   files   glob   def   count   目录名   

使用python遍历文件夹下的子文件夹及文件,并统计出文件夹下文件的数量:

 1 import os
 2 count = 0
 3 
 4 
 5 # 遍历文件夹
 6 def walkFile(file):
 7     for root, dirs, files in os.walk(file): 
 8         # root 表示当前正在访问的文件夹路径
 9         # dirs 表示该文件夹下的子目录名list
10         # files 表示该文件夹下的文件list
11 
12         # 遍历文件
13         for f in files:
14             global count
15             count += 1
16             print(os.path.join(root, f))
17 
18         # 遍历所有的文件夹
19         for d in dirs:
20             print(os.path.join(root, d))
21     print("文件数量一共为:", count)
22 
23 
24 if __name__ == __main__:
25     walkFile(r"E:\test")

 

python 遍历文件夹并统计文件数量

标签:wal   name   pre   pytho   files   glob   def   count   目录名   

原文地址:https://www.cnblogs.com/benben-wu/p/12411316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!