python 多级文件夹(未知几级,某级会有压缩文件夹),底层文件为.txt, 获取所有txt文件的路径

python 多级文件夹(未知几级,某级会有压缩文件夹),底层文件为.txt, 获取所有txt文件的路径

import os
import zipfile

# 路径
rootPath = r'F:\PycharmWorkPlace\databaseDemo\Test'


# 文件列表
def findTxtFiles(root):
    for file in os.listdir(root):
        path = os.path.join(root, file)
        if os.path.isdir(path):
            findTxtFiles(path)
        elif file.endswith('.txt'):
            print(path)
        elif file.endswith('.zip'):
            if not zipfile.is_zipfile(path):
                raise Exception('This is not a true zip file!')
            zipFile = zipfile.ZipFile(path, 'r')
            zipFile.setpassword(b'123')  # 密码:123
            for name in zipFile.namelist():
                try:
                    new_names = name.encode('cp437').decode('gbk')
                except:
                    new_names = name.encode('utf-8').decode('utf-8')
                subPath = os.path.join(path, new_names)
                if new_names.endswith('.txt'):
                    print(subPath)
                elif os.path.isdir(subPath) or file.endswith('.zip'):
                    findTxtFiles(subPath)


findTxtFiles(rootPath)