1、文件重复查找器
有没有看过你的硬盘,想过,为什么只剩下 100MB 了? 重复文件,大概率是因为这个原因。在这里有一个脚本可以找到重复文件并删除它们:
import os
import hashlib
def hash_file(filename):
h = hashlib.md5()
with open(filename, 'rb') as file:
while chunk := file.read(8192):
h.update(chunk)
return h.hexdigest()
def find_duplicates(folder):
hashes = {}
for dirpath, _, filenames in os.walk(folder):
for f in filenames:
full_path = os.path.join(dirpath, f)
file_hash = hash_file(full_path)
if file_hash in hashes:
print(f"Duplicate found: {full_path} == {hashes[file_hash]}")
else:
hashes[file_hash] = full_path
find_duplicates('/path/to/your/folder')
2、自动整理下载文件夹
下面这个脚本可以帮你整理下载文件夹:
import os
import shutil
def organize_folder(folder):
file_types = {
'Images': ['.jpeg', '.jpg', '.png', '.gif'],
'Videos': ['.mp4', '.avi', '.mov'],
'Documents': ['.pdf', '.docx', '.txt'],
'Archives': ['.zip', '.rar']
}
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
if os.path.isfile(file_path):
ext = os.path.splitext(filename)[1].lower()
for folder_name, extensions in file_types.items():
if ext in extensions:
target_folder = os.path.join(folder, folder_name)
os.makedirs(target_folder, exist_ok=True)
shutil.move(file_path, os.path.join(target_folder, filename))
print(f'Moved {filename} to {folder_name}')
organize_folder('/path/to/Downloads')
通常情况下没有人有时间去手动整理文件。
3、批量图像调整器
是否正在进行一个需要图像调整大小的项目吗?下面的脚本可以轻松帮您批量调整图片的大小:
from PIL import Image
import os
def batch_resize(folder, width, height):
for filename in os.listdir(folder):
if filename.endswith(('.jpeg', '.jpg', '.png')):
img = Image.open(os.path.join(folder, filename))
img = img.resize((width, height))
img.save(os.path.join(folder, f"resized_{filename}"))
print(f'Resized {filename}')
batch_resize('/path/to/images', 800, 600)