查看文件夹内各文件数目

 1 """
 2 This script shows how to count all files in a specific directory.
 3 """
 4 
 5 import os
 6 from collections import Counter
 7 
 8 DIR = "/home/robin/Documents/landmark/dataset/300VW_Dataset_2015_12_14"
 9 
10 
11 def get_extention(file_name=None):
12     """
13     Return the file name extention, or None if the file doesn't have one.
14     """
15     crumbs = file_name.split(".")
16     crumbs_num = len(crumbs)
17     if crumbs_num == 1:
18         return None
19     else:
20         return crumbs[-1]
21 
22 
23 def count_files(directory=None):
24     """
25     Count all files in directory, and return the dict contains the result.
26     """
27     file_extentions = []
28     none_extentions_num = 0
29     for _, _, files in os.walk(directory):
30         for file in files:
31             extention = get_extention(file)
32             if extention is None:
33                 none_extentions_num += 1
34             else:
35                 file_extentions.append(extention)
36     ext_counter = Counter(file_extentions)
37     if none_extentions_num != 0:
38         ext_counter.update({"None": none_extentions_num})
39     return ext_counter
40 
41 
42 def main():
43     """
44     The main entrance.
45     """
46     extention_dict = dict(count_files(DIR))
47     total_count = sum(extention_dict.values())
48     print("Total files:", total_count)
49     for _, name in enumerate(extention_dict):
50         print(name+":", extention_dict[name], end='; ')
51     print("Done!")
52 
53 
54 if __name__ == '__main__':
55     main()

查看文件夹内各文件数目

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值