zoukankan      html  css  js  c++  java
  • 查看文件夹内各文件数目

     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()

    查看文件夹内各文件数目

  • 相关阅读:
    896. 单调数列
    819. 最常见的单词
    collections.Counter()
    257. 二叉树的所有路径
    万里长征,始于足下——菜鸟程序员的学习总结(三)
    Ogre启动过程&原理
    Ogre导入模型
    四元数
    Ogre3D嵌入Qt框架
    如何搭建本地SVN服务
  • 原文地址:https://www.cnblogs.com/ansang/p/8432987.html
Copyright © 2011-2022 走看看