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

    查看文件夹内各文件数目

  • 相关阅读:
    常见http代码错误原因及处理
    tar命令详解
    Laravel笔记
    ORM要用到的数组转对象和对象转数组函数
    模块
    内置函数
    正则
    sorted 、 filter 、 map
    匿名函数、冒泡排序,二分法, 递归
    python 函数部分
  • 原文地址:https://www.cnblogs.com/ansang/p/8432987.html
Copyright © 2011-2022 走看看