Python 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!!