python改变文件修改时间
代码 :
import os,random import time,datetime def change_file_time(file_path, new_timestamp): # 使用stat函数获取文件的状态 file_stat = os.stat(file_path) # 更新文件的访问时间和修改时间 os.utime(file_path, (file_stat.st_atime, new_timestamp)) # 输出文件的新访问时间和修改时间 new_atime = os.path.getatime(file_path) new_mtime = os.path.getmtime(file_path) print(f"文件的新访问时间:{time.ctime(new_atime)}") print(f"文件的新修改时间:{time.ctime(new_mtime)}") fpath = input("请输入文件路径:") t1 = str(input("请输入修改日期yyyymmdd:")) now = datetime.datetime.now() new_date = now.replace(year=int(t1[0:4]) ,month=int(t1[4:6]), day=int(t1[6:8])) random_number = random.randrange(0, 3600*5) new_date = new_date + datetime.timedelta(seconds=random_number) nd_stamp = new_date.timestamp() change_file_time(fpath,nd_stamp)