import os
import datetime
import shutil
a = str(input("请输入需要整理图片的路径:"))
def organize_files_by_date(directory):
for filename in os.listdir(directory):
if os.path.isfile(os.path.join(directory, filename)):
# 获取文件的修改时间
modified_time = os.path.getmtime(os.path.join(directory, filename))
# 将修改时间转换为datetime对象
date_obj = datetime.datetime.fromtimestamp(modified_time)
# 提取datetime对象的年份和月份
year_month = date_obj.strftime('%Y-%m')
# 构造新的文件夹路径
new_folder = os.path.join(directory, year_month)
# 如果新文件夹不存在,则创建它
if not os.path.exists(new_folder):
os.makedirs(new_folder)
# 将文件移动到新文件夹中
shutil.move(os.path.join(directory, filename), os.path.join(new_folder, filename))
directory = a
organize_files_by_date(directory)
print("成功!!!")
如何用python整理图片,年份+月份
最新推荐文章于 2024-09-07 20:08:36 发布