Latest web development tutorials

Python os.utime () method

Python File (File) method Python OS file / directory methods


Outline

os.utime () method is used to set the path specified file last modification and access times.

In Unix, Windows effectively.

grammar

utime () method syntax is as follows:

os.utime(path, times)

parameter

  • path - a file path

  • times - If time is accessed and modified None, the file is set to the current time.Otherwise, time is a digital 2-tuple, (atime, mtime) were used as the access and modification time.

return value

This method has no return value.

Examples

The following example demonstrates the use of utime () method:

#!/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!!"

The above program output 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!!

Python File (File) method Python OS file / directory methods