统计一个文件夹内有多少zip文件,打印文件名(可以全路径)
#!/usr/bin/env python3
import os
def count_and_list_zip_files(folder_path):
zip_files_only = []
zip_files_all = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.zip'):
zip_files_only.append(file)
file_path = os.path.join(root, file) # 获取完整的文件路径
zip_files_all.append(file_path)
print(f'Total number of zip files: {len(zip_files_only)}')
print('List of zip files(只显示文件名):')
for zip_file in zip_files_only:
print(zip_file)
print('\n\n\nList of zip files(全路径):')
for zip_file in zip_files_all:
print(zip_file)
folder_path = '/Users/Desktop/tst'
count_and_list_zip_files(folder_path)
第十行zip可按需改为rar、doc、py等