Python os.utime() method
Python OS file/directory methods
Overview
The os.utime() method sets the last modification and access times of the file at the specified path.
Valid on Unix and Windows.
Syntax
The **utime()** method syntax is as follows:
os.utime(path, times)
Parameters
-
path -- The file path
-
times -- If times is None, the file's access and modification times are set to the current time. Otherwise, times is a 2-tuple of numbers, (atime, mtime), used as the access and modification times respectively.
Return Value
This method has no return value.
Example
The following example demonstrates the use of the utime() method:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys
# Display file stat information
stinfo = os.stat('a2.py')
print stinfo
# Use os.stat to get the file's access and modification times
print "a2.py 的访问时间: %s" %stinfo.st_atime
print "a2.py 的修改时间: %s" %stinfo.st_mtime
# Modify access and modification time
os.utime("a2.py",(1330712280, 1330712292))
print "done!!"
The output of the above program is:
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!!