无涯教程-OS File - os.utime(path, times)函数

177 阅读1分钟

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

os.utime(path, times) - 语法

下面是utime()方法-的语法

os.utime(path, times)
  • path     -  这是文件的路径。

  • times    -  这是文件访问和修改时间。如果times为None,则将文件访问和修改时间设置为当前时间。参数times由(atime,mtime)形式的行组成,即(accesstime,modifiedtime)。

os.utime(path, times) - 示例

以下示例显示了utime()方法的用法。

# !/usr/bin/python

import os, sys

# 显示文件的统计信息 stinfo=os.stat(a2.py) print stinfo

# 使用 os.stat 接收文件的 atime 和 mtime print "access time of a2.py: %s" %stinfo.st_atime print "modified time of a2.py: %s" %stinfo.st_mtime

# 修改 atime 和 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)
access time of a2.py: 1330498070
modified time of a2.py: 1330498074
done!!

参考链接

www.learnfk.com/python/os-u…