电脑用久了,桌面和下载文件夹就是重灾区——“新建文件夹 (1)” “新建文件夹 (2)” “最终版” “最终版2”……用 Python 几行代码就能批量整理干净。
一、批量重命名
1. 统一命名规则
import os
def batch_rename(directory, prefix="file", start=1, digits=3):
"""
批量重命名文件
directory: 文件夹路径
prefix: 文件名前缀
start: 起始编号
digits: 编号位数(如 3 → 001, 002)
"""
files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
# 按修改时间排序(可选)
files.sort(key=lambda x: os.path.getmtime(os.path.join(directory, x)))
for i, filename in enumerate(files):
# 获取文件扩展名
name, ext = os.path.splitext(filename)
new_name = f"{prefix}_{str(start + i).zfill(digits)}{ext}"
old_path = os.path.join(directory, filename)
new_path = os.path.join(directory, new_name)
os.rename(old_path, new_path)
print(f"{filename} → {new_name}")
# 使用
batch_rename("D:/照片/2026婚纱照", prefix="wedding", start=1, digits=3)
# 输出:IMG_001.jpg → wedding_001.jpg
batch_rename("D:/下载/课件", prefix="note", start=1, digits=2)
# 输出:新建 Microsoft Word 文档.docx → note_01.docx
2. 添加日期前缀
from datetime import datetime
def add_date_prefix(directory):
"""给文件添加日期前缀"""
today = datetime.now().strftime("%Y%m%d")
for f in os.listdir(directory):
filepath = os.path.join(directory, f)
if os.path.isfile(filepath) and not f.startswith(today):
new_name = f"{today}_{f}"
new_path = os.path.join(directory, new_name)
os.rename(filepath, new_path)
print(f"添加前缀: {f} → {new_name}")
# 使用:给今天下载的课件加日期前缀
add_date_prefix("D:/下载/课件")
3. 批量替换文件名中的文字
def replace_filename_text(directory, old_text, new_text):
"""批量替换文件名中的关键词"""
count = 0
for f in os.listdir(directory):
filepath = os.path.join(directory, f)
if os.path.isfile(filepath) and old_text in f:
new_name = f.replace(old_text, new_text)
new_path = os.path.join(directory, new_name)
os.rename(filepath, new_path)
print(f"替换: {f} → {new_name}")
count += 1
print(f"共替换 {count} 个文件")
# 使用:去掉文件名中的"【课程资料】"前缀
replace_filename_text("D:/课件", "【课程资料】", "")
# 统一空格
replace_filename_text("D:/下载", " ", " ") # 双空格变单空格
二、按类型自动归档
把杂乱的文件夹一键按文件类型分类整理:
import os
import shutil
EXTENSION_MAP = {
# 图片
".jpg": "图片", ".jpeg": "图片", ".png": "图片",
".gif": "图片", ".bmp": "图片", ".webp": "图片",
# 文档
".doc": "文档", ".docx": "文档", ".pdf": "文档",
".xls": "表格", ".xlsx": "表格",
".ppt": "演示文稿", ".pptx": "演示文稿",
".txt": "文本", ".md": "文本",
# 压缩包
".zip": "压缩包", ".rar": "压缩包", ".7z": "压缩包",
# 视频
".mp4": "视频", ".avi": "视频", ".mov": "视频",
# 代码
".py": "代码", ".java": "代码", ".js": "代码",
".html": "代码", ".css": "代码", ".sql": "代码",
}
def auto_sort_files(directory):
"""按文件类型自动归档到子文件夹"""
if not os.path.exists(directory):
print(f"文件夹不存在: {directory}")
return
moved_count = 0
for f in os.listdir(directory):
filepath = os.path.join(directory, f)
if not os.path.isfile(filepath):
continue
# 获取扩展名
_, ext = os.path.splitext(f)
ext = ext.lower()
# 确定目标文件夹
folder_name = EXTENSION_MAP.get(ext, "其他")
target_dir = os.path.join(directory, folder_name)
os.makedirs(target_dir, exist_ok=True)
# 移动文件
target_path = os.path.join(target_dir, f)
if not os.path.exists(target_path):
shutil.move(filepath, target_path)
moved_count += 1
else:
# 同名文件加编号
name, ext = os.path.splitext(f)
target_path = os.path.join(target_dir, f"{name}_1{ext}")
shutil.move(filepath, target_path)
moved_count += 1
print(f"整理完成!{moved_count} 个文件已归档")
# 使用
auto_sort_files("D:/下载")
三、删除重复文件
1. 按大小和名称删除
def find_duplicates(directory):
"""查找重复文件(同名且同大小)"""
files_info = {}
duplicates = []
for root, _, files in os.walk(directory):
for f in files:
filepath = os.path.join(root, f)
size = os.path.getsize(filepath)
# 用 (文件名, 大小) 作为 key
key = (f, size)
if key in files_info:
duplicates.append((filepath, files_info[key]))
else:
files_info[key] = filepath
return duplicates
def delete_duplicates(directory, dry_run=True):
"""删除重复文件(dry_run=True 只预览不删除)"""
duplicates = find_duplicates(directory)
if not duplicates:
print("没有找到重复文件")
return
print(f"找到 {len(duplicates)} 组重复文件:\n")
for dup, original in duplicates:
print(f" 保留: {original}")
print(f" 删除: {dup}\n")
if not dry_run:
os.remove(dup)
if dry_run:
print(f"以上是预览结果,共可清理 {len(duplicates)} 个文件")
print(f"确认删除请运行: delete_duplicates('{directory}', dry_run=False)")
else:
print(f"已删除 {len(duplicates)} 个重复文件")
# 使用
delete_duplicates("D:/照片", dry_run=True)
2. 按内容哈希删除(更准确)
连不同名的重复内容也能找出来:
import hashlib
def find_duplicates_by_hash(directory):
"""通过文件内容哈希查找重复文件"""
hash_map = {}
duplicates = []
for root, _, files in os.walk(directory):
for f in files:
filepath = os.path.join(root, f)
try:
with open(filepath, "rb") as fh:
file_hash = hashlib.md5(fh.read()).hexdigest()
except:
continue # 跳过不能读的文件
if file_hash in hash_map:
duplicates.append((filepath, hash_map[file_hash]))
else:
hash_map[file_hash] = filepath
return duplicates
四、按日期归档
import os
import shutil
from datetime import datetime
def archive_by_month(directory, file_extensions=None):
"""按年月归档文件(适合照片、截图等)"""
if file_extensions is None:
file_extensions = [".jpg", ".jpeg", ".png", ".gif", ".mp4"]
moved = 0
for f in os.listdir(directory):
filepath = os.path.join(directory, f)
if not os.path.isfile(filepath):
continue
# 筛选文件类型
_, ext = os.path.splitext(f)
if ext.lower() not in file_extensions:
continue
# 获取文件修改时间
mtime = os.path.getmtime(filepath)
file_date = datetime.fromtimestamp(mtime)
year_month = file_date.strftime("%Y年%m月")
# 创建目标文件夹
target_dir = os.path.join(directory, year_month)
os.makedirs(target_dir, exist_ok=True)
# 移动
shutil.move(filepath, os.path.join(target_dir, f))
moved += 1
print(f"已归档 {moved} 个文件到 {year_month} 文件夹")
五、清空空文件夹
def remove_empty_dirs(directory):
"""递归删除空文件夹"""
removed = 0
for root, dirs, files in os.walk(directory, topdown=False):
for d in dirs:
dirpath = os.path.join(root, d)
try:
if not os.listdir(dirpath):
os.rmdir(dirpath)
print(f"删除空文件夹: {dirpath}")
removed += 1
except:
pass
print(f"共删除 {removed} 个空文件夹")
六、一键整理桌面
def clean_desktop():
"""一键整理桌面"""
desktop = os.path.expanduser("~/Desktop")
print(f"整理桌面: {desktop}")
# 1. 按类型归档
auto_sort_files(desktop)
# 2. 删除空文件夹
remove_empty_dirs(desktop)
# 3. 查找重复文件
duplicates = find_duplicates(desktop)
if duplicates:
print(f"桌面有 {len(duplicates)} 组重复文件")
for d, o in duplicates[:5]:
print(f" {os.path.basename(d)}")
else:
print("无重复文件")
print("桌面整理完成!")
# 使用(谨慎:会移动桌面上所有文件)
# clean_desktop()
七、文件整理工具箱
class FileOrganizer:
"""文件整理工具箱"""
def __init__(self, base_dir):
self.base_dir = base_dir
def preview(self):
"""预览当前文件统计"""
total = 0
ext_count = {}
for f in os.listdir(self.base_dir):
if os.path.isfile(os.path.join(self.base_dir, f)):
total += 1
_, ext = os.path.splitext(f)
ext_count[ext] = ext_count.get(ext, 0) + 1
print(f"📁 {self.base_dir}")
print(f"📄 共 {total} 个文件")
for ext, count in sorted(ext_count.items(), key=lambda x: -x[1])[:10]:
print(f" {ext}: {count}个")
def organize(self):
"""一键整理"""
print("开始整理...")
auto_sort_files(self.base_dir)
remove_empty_dirs(self.base_dir)
print("整理完成!")
# 使用
org = FileOrganizer("D:/下载")
org.preview()
org.organize()
总结
文件整理最常用的三个场景:
batch_rename("文件夹", "前缀") # 批量重命名
auto_sort_files("文件夹") # 按类型归档
find_duplicates("文件夹") # 查找重复
建议先 dry_run=True 预览效果,确认没问题再实际执行。不要一上来就跑删除命令,血的教训。
💡 觉得有用的话,点赞 + 关注【张老师技术栈】吧!每周更新 Java/Python/爬虫 实战干货,不让你白来。

360

被折叠的 条评论
为什么被折叠?



