本帖最后由 llyaomo 于 2024-6-21 16:27 编辑
如题,我百度智能生成了两段代码,第一段目的是将文件按照修改日期分类整理,第二段目的是将文件夹按照修改日期分类整理,其实我起初的目标是将两个功能合二为一,可是百度AI不够聪明,实现不了。现在求助会PY的各位大佬,帮我合并两段代码,功能合二为一,不胜感激。
import os
import shutil
import hashlib
from datetime import datetime
import tkinter as tk
from tkinter import filedialog
# 创建一个Tk窗口实例
root = tk.Tk()
root.withdraw() # 隐藏主窗口
# 打开文件夹选择对话框
source_folder = filedialog.askdirectory()
# 打印选择的文件夹路径
if source_folder:
print("输入的待整理文件夹地址是:", source_folder)
else:
print("未选择文件夹")
# 打开文件夹选择对话框
destination_folder = filedialog.askdirectory()
# 打印选择的文件夹路径
if destination_folder:
print("输入的目标文件夹地址是:", destination_folder)
else:
print("未选择文件夹")
def get_week_and_year(mtime):
"""根据修改时间获取年份和周数"""
date = datetime.fromtimestamp(mtime)
year = date.year
month = date.month
week = date.strftime('%V') # ISO 8601 周数
return year, month, week
def get_file_md5(file_path):
"""获取文件的MD5哈希值"""
hash_md5 = hashlib.md5()
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def organize_files_by_date(source_dir, dest_dir):
"""按照修改日期组织文件和文件夹,并处理同名文件的md5冲突"""
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
for item in os.listdir(source_dir):
item_path = os.path.join(source_dir, item)
if os.path.isfile(item_path):
mtime = os.path.getmtime(item_path)
year, month, week = get_week_and_year(mtime)
path1 = '年'
path3 = '周'
dest_base_path = os.path.join(dest_dir, str(year)+path1, f"{month:02d}月", str(week)+path3)
if not os.path.exists(dest_base_path):
os.makedirs(dest_base_path)
dest_file_path = os.path.join(dest_base_path, item)
if os.path.exists(dest_file_path):
# 如果目标文件已存在,比较MD5
dest_md5 = get_file_md5(dest_file_path)
source_md5 = get_file_md5(item_path)
if dest_md5 == source_md5:
# MD5相同,选择覆盖
shutil.move(item_path, dest_file_path)
else:
# MD5不同,选择重命名
base, ext = os.path.splitext(item)
counter = 1
new_dest_file_path = dest_file_path
while os.path.exists(new_dest_file_path):
new_dest_file_path = os.path.join(dest_base_path, f"{base}_{counter}{ext}")
counter += 1
shutil.move(item_path, new_dest_file_path)
else:
# 目标文件不存在,直接复制
shutil.move(item_path, dest_file_path)
# 使用示例
# source_folder = input("请输入需要整理的文件夹路径: ")
# destination_folder = input("请输入目标文件夹路径: ")
organize_files_by_date(source_folder, destination_folder)
import os
import shutil
import datetime
import tkinter as tk
from tkinter import filedialog
# 创建一个Tk窗口实例
root = tk.Tk()
root.withdraw() # 隐藏主窗口
# 打开文件夹选择对话框
source_directory = filedialog.askdirectory()
# 打印选择的文件夹路径
if source_directory:
print("输入的待整理文件夹地址是:", source_directory)
else:
print("未选择文件夹")
# 打开文件夹选择对话框
destination_directory = filedialog.askdirectory()
# 打印选择的文件夹路径
if destination_directory:
print("输入的目标文件夹地址是:", destination_directory)
else:
print("未选择文件夹")
def get_year_month_week(folder_path):
mtime = os.path.getmtime(folder_path)
modified_date = datetime.datetime.fromtimestamp(mtime)
year = modified_date.year
month = modified_date.month
week = modified_date.isocalendar()[1]
return year, month, week
def move_or_rename_folder(source_folder, dest_path):
# 获取源文件夹的详细信息
source_name = os.path.basename(source_folder)
source_size = sum(
os.path.getsize(os.path.join(root, file)) for root, dirs, files in os.walk(source_folder) for file in files)
# 检查目标路径是否存在同名文件夹
target_folder = os.path.join(dest_path, source_name)
if os.path.exists(target_folder):
# 如果存在,比较大小
target_size = sum(
os.path.getsize(os.path.join(root, file)) for root, dirs, files in os.walk(target_folder) for file in files)
if source_size == target_size:
print(f"Skipping {source_folder} because a folder with the same name and size already exists.")
return
# 如果不存在同名文件夹或大小不同,移动或重命名文件夹
base_name, ext = os.path.splitext(source_name)
counter = 1
new_name = source_name
while os.path.exists(os.path.join(dest_path, new_name)):
new_name = f"{base_name}_{counter}{ext}"
counter += 1
shutil.move(source_folder, os.path.join(dest_path, new_name))
print(f"Moved or renamed {source_folder} to {os.path.join(dest_path, new_name)}")
def organize_folders_by_date(source_dir, dest_dir):
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
for item in os.listdir(source_dir):
item_path = os.path.join(source_dir, item)
if os.path.isdir(item_path):
year, month, week = get_year_month_week(item_path)
# 创建目标文件夹路径
target_path = os.path.join(dest_dir, f"{year}年", f"{month:02d}月", f"{week:02d}周")
# 确保目标路径存在
if not os.path.exists(target_path):
os.makedirs(target_path)
# 移动或重命名文件夹
move_or_rename_folder(item_path, target_path)
# 使用示例
# source_directory = input("请输入源文件夹地址:") # 源文件夹路径
# destination_directory = input("请输入目标文件夹地址:") # 目标文件夹路径
organize_folders_by_date(source_directory, destination_directory)
|