求一种文件分类的脚本或者软件 - 『悬赏问答区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn
之前回帖的脚本修改了下,使用tk弹框获取链接,控制台交互
[Python] 纯文本查看 复制代码 import os
import shutil
import tkinter as tk
from tkinter import filedialog
# 获取文件夹总大小、文件数量、单个文件大小
def get_folder_info(folder):
total_size = 0
file_count = 0
files_with_size = {}
for dirpath, dirnames, filenames in os.walk(folder):
for f in filenames:
fp = os.path.join(dirpath, f)
if os.path.isfile(fp):
file_size = os.path.getsize(fp)
files_with_size[f] = file_size
total_size += file_size
file_count += 1
return total_size, file_count, files_with_size
# 分配文件
def new_files(path,total_capacity,Number_files):
count=0
# path父级目录
parent_folder = os.path.dirname(path)
while True:
print(f'开始分组,当前第:{count+1}组')
data=0
l=0
lod_files=get_folder_info(path)
if int(lod_files[1])>0:
subfolder_name = os.path.join(parent_folder, f"Subfolder11_{count}")
os.makedirs(subfolder_name, exist_ok=True)
sorted_files = sorted(lod_files[2].items(), key=lambda x: x[1], reverse=True)
for name,file_size in sorted_files:
l+=1
file_path = os.path.join(path, name)
data+=file_size
if data//( 1024 * 1024 * 1024)>total_capacity:
break
shutil.move(file_path,subfolder_name)
if l >Number_files:
break
count+=1
if l>200:
break
else:
break
# 删除空文件夹
def delete_empty_folders(folder):
for folder_name in os.listdir(folder):
folder_path = os.path.join(folder, folder_name)
if os.path.isdir(folder_path):
if not os.listdir(folder_path): # 检查文件夹是否为空
os.rmdir(folder_path) # 删除空文件夹
if __name__ == "__main__":
# 创建主窗口
root = tk.Tk()
root.withdraw() # 隐藏主窗口
# 创建文件夹选择对话框
path = filedialog.askdirectory(title='请选择文件夹')
for _ in range(5):
if os.path.isdir(path):
print("选择的文件夹是:", path)
break
else:
print("请选择文件夹!")
total_capacity = input("文件夹最大大小,单位GB,回车默认20:") or 20
print("输入的最大大小是:", total_capacity)
Number_files= input("文件夹放入文件的个数,回车默认10:") or 10
print("输入文件夹放入文件的个数是:", Number_files)
# print(f'当前操作文件夹路径:{path}')
# 获取处理文件夹的信息
lod_files=get_folder_info(path)
print(f"当前文件夹总大小:{lod_files[0]//(1024 * 1024)} MB")
print(f'当前操作文件夹文件数量:{lod_files[1]}')
new_files(path,total_capacity,Number_files)
root.destroy()
# 运行主循环
root.mainloop() |