Python3 os.utime() 方法
概述
os.utime() 方法用於設置指定路徑文件最後的修改和訪問時間。
在Unix,Windows中有效。
語法
utime()方法語法格式如下:
os.utime(path, times)
參數
path --文件路徑
times --如果時間是None,則文件的訪問和修改設為當前時間。否則, 時間是一個2-tuple數字, (atime, mtime) 用來分別作為訪問和修改的時間。
返回值
該方法沒有返回值。
實例
以下實例演示了utime() 方法的使用:
#!/usr/bin/python # -*- coding: UTF-8 -*- import os, sys # 显示文件的 stat 信息 stinfo = os.stat('a2.py') print (stinfo) # 使用 os.stat 来接收文件的访问和修改时间 print ("a2.py 的访问时间: %s" %stinfo.st_atime) print ("a2.py 的修改时间: %s" %stinfo.st_mtime) # 修改访问和修改时间 os.utime("a2.py",(1330712280, 1330712292)) print ("done!!")
執行以上程序輸出結果為:
posix.stat_result(st_mode=33188, st_ino=3940649674337682L, st_dev=277923425L, st _nlink=1, st_uid=400, st_gid=401, st_size=335L, st_atime=1330498070, st_mtime=13 30498074, st_ctime=1330498065) a2.py 的访问时间: 1330498070 a2.py 的修改时间: 1330498074 done!!