利用python求出一个文件夹下的各种文件的个数和类型

"""
This script shows how to count all files in a specific directory.
"""

import os
from collections import Counter

DIR = "./imgs"


def get_extention(file_name=None):
    """
    Return the file name extention, or None if the file doesn't have one.
    """
    crumbs = file_name.split(".")
    crumbs_num = len(crumbs)
    if crumbs_num == 1:
        return None
    else:
        return crumbs[-1]


def count_files(directory=None):
    """
    Count all files in directory, and return the dict contains the result.
    """
    file_extentions = []
    none_extentions_num = 0
    for _, _, files in os.walk(directory):
        # print(files)
        for file in files:
            extention = get_extention(file)
            if extention is None:
                none_extentions_num += 1
            else:
                file_extentions.append(extention)
    ext_counter = Counter(file_extentions)
    if none_extentions_num != 0:
        ext_counter.update({"None": none_extentions_num})
    return ext_counter


def main():
    """
    The main entrance.
    """
    # print(count_files(DIR))
    extention_dict = dict(count_files(DIR))
    total_count = sum(extention_dict.values())
    print("Total files:", total_count)
    for _, name in enumerate(extention_dict):
        print(name+":", extention_dict[name], end='; ')
    print("Done!")


if __name__ == '__main__':
    main()

相比直接浏览得到的信息更加全面。

猜你喜欢

转载自blog.csdn.net/fanzonghao/article/details/89488168