感觉还蛮好用的:)
2024/2/26:生成文件夹没有文件则删除该文件夹
import os
import shutil
from pathlib import Path
# 桌面路径
desktop_path = Path(r"D:\yzzob\Desktop")
# 创建存放不同类型文件的文件夹
folders = {
"txt": "TextFiles",
"exe": "ExecutableFiles",
"jpg""png": "ImageFiles",
"png": "ImageFiles",
"xlsx": "DocFile",
"docx": "DocFile",
"pcap": "CtfAttachments",
"pcapng": "CtfAttachments",
"":"CtfAttachments",
"raw":"CtfAttachments",
"til":"IdaFile",
"id0":"IdaFile",
"nam":"IdaFile",
"id1":"IdaFile",
"id2":"IdaFile"
}
for folder_name in folders.values():
folder_path = desktop_path / folder_name
folder_path.mkdir(exist_ok=True)
# 移动文件到对应的文件夹
for file_path in desktop_path.iterdir():
if file_path.is_file():
file_extension = file_path.suffix.lower()[1:]
if file_extension in folders:
destination_folder = desktop_path / folders[file_extension]
target_path = destination_folder / file_path.name
# 检查目标路径是否存在同名文件
if target_path.exists():
# 重命名文件
current_date = datetime.datetime.now().strftime("%Y%m%d_%H_%M_%S")
new_name = file_path.stem + "_" + current_date + file_path.suffix
target_path = destination_folder / new_name
shutil.move(str(file_path), str(target_path))
# 删除空文件夹
for folder_name in folders.values():
folder_path = desktop_path / folder_name
if folder_path.exists():
if not any(folder_path.iterdir()):
folder_path.rmdir()
本文介绍了一个Python脚本,用于在桌面路径下创建并管理不同类型的文件夹,如文本、执行文件、图像等,并将文件自动移动到相应的文件夹中,避免同名文件冲突,最后删除空文件夹。
3299

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



