pu369com

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)    
复制代码

 

posted on   pu369com  阅读(180)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示