##作业
##1、找出当前目录下举例上次修改时间超过3个月的文件
##2、将所有文件重命名,在元文件名的开头加上最后修改日期
##3、创建一个新文件夹叫backup
##4、将所有重命名后的文件添加到压缩包里,给压缩包名字加上今天日期
##5、夹压缩包移动到backup文件夹里面
##6、删掉原始文件
import os
import zipfile
import shutil
import zipfile
import datetime
files=os.scandir()
time_now=datetime.datetime.now()
if os.path.exists('tmp.zip'):
os.remove('tmp.zip')
for file in files:
time=datetime.datetime.fromtimestamp(os.stat(file).st_mtime)
monthdiff=(time_now.year-time.year)*12+time_now.month-time.month
## print(monthdiff,type(monthdiff))
if not os.path.isdir(file.path):
if monthdiff>3:
os.rename(file,f'{time_now.year}-{time_now.month}-{time_now.day}-{file.name}')
zipfile.ZipFile('tmp.zip','a').write(f'{time_now.year}-{time_now.month}-{time_now.day}-{file.name}')
os.remove(f'{time_now.year}-{time_now.month}-{time_now.day}-{file.name}')
if not os.path.exists('backup'):
os.mkdir('backup')
if os.path.exists('tmp.zip'):
shutil.move('tmp.zip',f'backup\{time_now.year}-{time_now.month}-{time_now.day}.zip')
else: print('没有修改日期超过3个月的文件')