前言

一个看起来好像没什么用的小程序,禁转
给定某文件夹所在路径,导出当前路径内包含的所有文件至excel表,并进行字符串递增排序。
调用库,看前建议去了解一下相关库的使用方法。

import os
import xlwt

代码

import os
import xlwt


class FileType:
    type = []  # 存放已有文件类型后缀

    def __init__(self, name):
        self.type_name = name
        self.file_list = []  # 存放该类型的文件
        FileType.type.append(name)  # 增添类型

    def l_sort(self):
        self.file_list.sort()  # 对该类型进行排序

    @classmethod
    def find_type(cls, type_n):  # 判断该类型是否已经存在
        if type_n in cls.type:
            return False
        return True

    def type_sort(self):
        self.file_list.sort()

    def get_file_num(self):
        return len(self.file_list)


# 这个函数返回一个 File_Type 的列表,即下面的 type_c 列表
def scan_directory():
    type_c = []  # 存放类
    while 1:
        dirs = input("Please input the path of aimed directories: ")
        f = []  # 存放文件路径
        d = []  # 存放文件夹路径
        if os.path.exists(dirs):
            files = os.listdir(dirs)
            for i in files:  # 遍历文件夹,将文件路径和文件夹路径分别存在两个列表里面
                tmp = os.path.join(dirs, i)  # 将当前文件名与上级路径绑定
                if os.path.isfile(tmp):
                    f.append(tmp)
                else:
                    d.append(tmp)
            while len(d):  # 遍历所有子文件夹的文件,将其存入文件列表
                temp_file_list = os.listdir(d[0])
                for i in temp_file_list:
                    tmp = os.path.join(d[0], i)
                    if os.path.isfile(tmp):
                        f.append(tmp)
                    else:
                        d.append(tmp)
                d.pop(0)

            # type_c = [] # 存放类
            for ff in f:
                t = ff.rfind('.')
                s_tmp = ff[t + 1:]  # 找到文件后缀
                if FileType.find_type(s_tmp):  # 如果能在类里面找不到该文件类型,那么就新建一个文件类型
                    K = FileType(s_tmp)
                    K.file_list.append(ff)  # 将当前文件路径存入
                    type_c.append(K)  # 存入类列表
                else:
                    i = 0  # 索引
                    cnt = 0
                    for n in type_c:  # 找到该文件类型对应的类
                        if n.type_name == s_tmp:
                            i = cnt  # 记录索引
                        cnt += 1
                    type_c[i].file_list.append(ff)  # 在当前类里面存入相应的文件路径

            # 对每个类型下的文件路径进行字符串递增排序
            for i in type_c:
                i.type_sort()
            return type_c
            break
        else:
            print("Wrong file path!")

# 生成excel
def write_excel(c):
    s = input("Please enter the path where you want to save the file:")
    newxlsx = xlwt.Workbook()
    for cc in c:
        cnt = 0
        newsheet = newxlsx.add_sheet(cc.type_name)
        for t in cc.file_list:
            newsheet.write(cnt, 0, t)
            cnt += 1
    s += "/excel_File.xls"
    newxlsx.save(s)
    print("You've saved the file to \"%s\"." % s)


if __name__ == '__main__':
    type_c = scan_directory()
    write_excel(type_c)
    print('Finish!')

''' Please input the path of aimed directories: E:\学习 Please enter the path where you want to save the file:E: You've saved the file in "E:/excel_File.xls" Finish! '''

运行结果

本文地址:https://blog.csdn.net/qq_45750017/article/details/110295996