目录
如果你使用了Anaconda,请先切换环境!!!
安装依赖
pip install tqdm aiofiles
代码
新建一个find.py文件,将以下代码粘贴进去:
import os
import datetime
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
def process_file(file_paths, year):
count = 0
total_size = 0
for file_path in file_paths:
try:
file_time = os.path.getmtime(file_path)
if year == "":
total_size += os.path.getsize(file_path)
count += 1
continue
if datetime.datetime.fromtimestamp(file_time).year == year:
total_size += os.path.getsize(file_path)
count += 1
except OSError as e:
print(f"Error processing file {file_path}: {e}")
return count, total_size
def find_files(target_dir, year, batch_size=500):
total_count = 0
total_size = 0
with ThreadPoolExecutor() as executor:
futures = []
file_paths = []
with tqdm(desc='正在收集文件', unit='个') as progress:
for root, dirs, files in os.walk(target_dir, topdown=True):
for file in files:
file_path = os.path.join(root, file)
file_paths.append(file_path)
progress.update(1)
if len(file_paths) >= batch_size:
futures.append(executor.submit(process_file, file_paths, year))
file_paths = []
# 添加剩余的 file_paths
futures.append(executor.submit(process_file, file_paths, year))
for f in tqdm(futures, desc=f"处理任务(每{batch_size}个文件为一个任务)", unit="个"):
count, size = f.result()
total_count += count
total_size += size
return total_count, total_size
# 获取用户输入
target_directory = input("请输入目标目录: ")
year = input("请输入年份(直接回车表示全部): ")
s = "全部"
if year != "":
s = f"{year}年"
year = int(year)
file_count, total_size = find_files(target_directory, year, batch_size=1000)
print("------------------------统计结果------------------------")
print(f"{s}文件总数量: {file_count}")
print(f"{s}文件总占用: {(total_size / (1024 * 1024)):.2f} M | {(total_size / (1024 * 1024 * 1024)):.2f} G")
print("------------------------------------------------")
os.system('pause')
运行
python find.py
打包为可执行程序
安装pyinstaller
pip install pyinstaller
然后运行以下命令,在运行的目录中有一个dist文件夹,可执行程序就在其中。
pyinstaller --onefile find.py
可执行程序已提供,可自行下载:
👍点赞,你的认可是我创作的动力 !
🌟收藏,你的青睐是我努力的方向!
✏️评论,你的意见是我进步的财富!