Python3 os.utime()方法

mtime()方法设置由path指定的文件的访问和修改时间。

语法

以下是 utime()方法的语法:
os.utime(path, times)

参数

  • path -- 这是该文件的路径

  • times -- 这是要设置的文件访问和修改时间。如果time没有提供,则文件访问和修改时间设置为当前时间。参数 times 由排在(atime, mtime)即(访问时间,修改时间)的形式。

返回值

此方法不返回任何值。

示例

下面的例子显示 utime() 方法的使用。
# !/usr/bin/python3

import os, sys, time
os.chdir("d:\\tmp")
# Showing stat information of file
stinfo = os.stat('foo.txt')
print (stinfo)

# Using os.stat to recieve atime and mtime of file
print ("access time of foo.txt: %s" %stinfo.st_atime)
print ("modified time of foo.txt: %s" %stinfo.st_mtime)
print (time.asctime( time.localtime(stinfo.st_atime)))
# Modifying atime and mtime
os.utime("foo.txt",(1330712280, 1330712292))
print ("after modification")
print (time.asctime( time.localtime(stinfo.st_atime)))
print ("done!!")

当我们运行上面的程序,它会产生以下结果:
os.stat_result(st_mode=33206, st_ino=1688849860351098, st_dev=1017554828, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1455684273, st_mtime=1455684273, st_ctime=1455683589)
access time of foo.txt: 1455684273.84375
modified time of foo.txt: 1455684273.84375
Wed Feb 17 10:14:33 2016
after modification
Fri Mar  2 23:48:00 2012
done!!

上一篇: Python3文件方法 下一篇: Python3 os文件目录的方法