本帖最后由 AbleFeng 于 2024-1-4 00:09 编辑
【软件介绍】
Python文件夹整理命名合并小工具。本来是想从150多个子文件夹中提取出我想要的1920x1080以上的壁纸图片,所以就搞了个小脚本,顺便把它打包成了带界面小工具,使用该工具,您可以一键完成文件夹的整理把需要的文件类型保存,把不需要的文件批量删除;文件按子文件夹名字重命名操作,无需手动逐个处理。它支持把子文件夹中所有文件合并移动到总文件夹中,让您能够灵活地对文件进行批量处理。
【使用说明】
1、拖拽文件夹到输入框然后设置要保留的文件扩展名
2、如果需要按照文件大小决定保留哪些文件就勾选并设置该项
3、如果需要按照图片尺寸决定保留哪些图片就勾选并设置该项
扩展名随便输入,支持大小写字母数字随便用中文或符号隔开就行,另外按子文件夹命名和全部移动到总文件夹的两个功能就没啥介绍的了,操作前记得备份好您的文件再进行测试使用;至于界面嘛不想用Pyside这个太大,然而tk又一言难尽,就随便写写吧!第一次发帖有不妥的各位大佬多多指出。
单文件打包11M大小:https://www.alipan.com/s/nVMhBEQa5q1
主操作源码贴出学习学习:
import os
from PIL import Image
import shutil
def delete_files(folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
if not is_image(file_path):
os.remove(file_path)
else:
if not is_large_enough(file_path, 1920, 1080):
os.remove(file_path)
else:
print(f"Keeping: {file_path}")
def rename_files(folder_path):
for root, dirs, files in os.walk(folder_path):
for idx, file in enumerate(files):
file_path = os.path.join(root, file)
extension = os.path.splitext(file_path)[1]
if len(files) == 1:
new_file_name = os.path.basename(root) + extension
else:
new_file_name = os.path.basename(root) + '_' + str(idx+1) + extension
new_file_path = os.path.join(root, new_file_name)
os.rename(file_path, new_file_path)
def move_files(folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
new_file_path = os.path.join(folder_path, file)
counter = 1
while os.path.exists(new_file_path):
file_name, file_extension = os.path.splitext(file)
new_file_name = f"{file_name}_{counter}{file_extension}"
new_file_path = os.path.join(folder_path, new_file_name)
counter += 1
shutil.move(file_path, new_file_path)
for root, dirs, files in os.walk(folder_path, topdown=False):
for dir in dirs:
dir_path = os.path.join(root, dir)
shutil.rmtree(dir_path)
def is_image(file_path):
image_extensions = ['.jpg', '.jpeg', '.png']
return any(file_path.lower().endswith(ext) for ext in image_extensions)
def is_large_enough(file_path, min_width, min_height):
try:
with Image.open(file_path) as img:
width, height = img.size
return width >= min_width and height >= min_height
except (IOError, OSError):
return False
if __name__ == '__main__':
folder_path = r'C:\Users\Administrator\Desktop\OEM\ACER'
delete_files(folder_path)
|