思路:
- 包装path对象,创建目录
- 循环遍历所有文件,获取每一文件的属性,并创建日期目录
- 移动文件
from pathlib import Path
import os
import shutil
import time
dir1 = r'D:\upty\test'
dir2 = r'D:\upty\work'
path1 = Path(dir1)
path2 = Path(dir2)
if not path2.exists():
path2.mkdir()
plist = list(path1.glob('*'))
for p in plist:
with open(p,'rb') as file:
info = os.stat(p)
print(info)
t = str(time.localtime(info.st_mtime).tm_year)+"-"+str(time.localtime(info.st_mtime).tm_mon)
rpath = path2/t
if not rpath.exists():
rpath.mkdir()
rfile = rpath/p.name
p.replace(rfile)
|