You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
948 B

import os
import openpyxl
# 设置文件大小阈值为50MB
FILE_SIZE_THRESHOLD = 50 * 1024 * 1024 # 50MB
# 初始化Excel工作簿
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "Files larger than 50MB"
ws.append(["File Path", "Size (MB)"])
# 遍历C盘文件系统
for root, dirs, files in os.walk("C:\\"):
for file in files:
file_path = os.path.join(root, file)
try:
file_size = os.path.getsize(file_path)
if file_size > FILE_SIZE_THRESHOLD:
filesize=file_size / (1024 * 1024)
ws.append([file_path,filesize]) # 以MB为单位记录文件大小
print("文件路径:{},文件大小:{}",file_path,filesize)
except (OSError, PermissionError):
# 忽略无法访问的文件
continue
# 保存结果到Excel文件
output_file = "D:\\large_files.xlsx"
wb.save(output_file)
print(f"结果已保存到 {output_file}")