python - 文件夹整理

将当前文件夹的文件根据创建时间进行整理

# 整理当前文件夹
import time
import os
import shutil


# 将旧文件复制到新路径
def cutFile(fileName):
    if os.path.isdir(fileName) == True:
        return
    if fileName == "scan.py" or fileName == "scan.bat":
        return
    filemt = time.localtime(os.stat(fileName).st_mtime)
    year = time.strftime("%Y", filemt)
    month = time.strftime("%m", filemt)
    day = time.strftime("%d", filemt)
    # 扫描文件目录创建
    scanSavePath = f"./{year}/{month}/{day}/"
    if not os.path.isdir(scanSavePath):
        print(f"创建目录: {scanSavePath}")
        os.makedirs(scanSavePath)
    shutil.move(fileName, scanSavePath + fileName)


while True:
    nowTime = f"{time.strftime('%H:%M:%S', time.localtime())}"

    dirs = os.listdir("./")
    for i in range(len(dirs)):
        fileName = dirs[i]
        cutFile(fileName)

    print(f"{nowTime}: 整理成功,休眠1小时")
    time.sleep(3600)

posted @ 2023-11-03 10:37  wstong  阅读(12)  评论(0编辑  收藏  举报