[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
# [url=home.php?mod=space&uid=238618]@Time[/url] : 2022-03-15 13:31
# [url=home.php?mod=space&uid=686208]@AuThor[/url] : qc
# [url=home.php?mod=space&uid=267492]@file[/url] : sortbydate.py
# @Software: PyCharm
import glob
import pathlib
import os
import datetime
import traceback
from concurrent import futures
import time
from functools import partial
'''
根据文件创建文件建立文件夹
'''
def to_str_path(path):
if path:
new_path = os.fspath(path)
return new_path
def movefile(image_path, dir_path):
try:
file_time = os.path.getctime(image_path)
stamp_to_datetime = datetime.datetime.fromtimestamp(file_time)
file_date = datetime.datetime.strftime(stamp_to_datetime, "%Y%m")
print(stamp_to_datetime)
if file_date <'202201':
date_dir = (dir_path.joinpath(file_date))
jpg_dir_path, jpg_name = os.path.split(image_path)
if not os.path.exists(date_dir):
os.makedirs(to_str_path(date_dir))
print(f"create path:{file_date},curr path:{date_dir}")
new_file_path = to_str_path(os.path.join(date_dir, jpg_name))
print(f"move file:{jpg_name},complete path: {new_file_path}")
os.rename(image_path, new_file_path) # 移动文件或重命名,这里是移动文件
print(f"file is move in {date_dir} path")
except:
print(traceback.format_exc())
def start(filename, filetype: str = "*"):
'''
:param filename: 指定文件夹名,默认当前py文件所在的文件夹
:param date_str: 删除日期格式yyyymmdd
:param filetype: 需要删除的文件类型默认jpg.
:return: None
'''
filename = filename or __file__
dir_path = pathlib.Path(filename).resolve().parent
print(dir_path)
image_path_list = glob.glob(to_str_path(dir_path.joinpath(f"*.{filetype}")))
movef = partial(movefile, dir_path=dir_path)
with futures.ThreadPoolExecutor(max_workers=10) as pool:
pool.map(movef, image_path_list)
if __name__ == '__main__':
start_t = time.time()
# filename = "D:\\360downloads\\1111"
# filename = input("=====Please input filename:=====\n")
file_list = ['D:\\360downloads\\1111','D:\\360downloads\\1111','D:\\360downloads\\1111']
for filename in file_list:
start(filename)
end = time.time()
print(end - start_t)
|