Python os.utime() Method

Python File(文件) 方法Python OS File/Directory Methods


Overview

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

Valid on Unix and Windows.

Syntax

utime()The method syntax is as follows:

os.utime(path, times)

Parameters

  • path-- file path

  • times-- If time is None, the file's access and modification times are set to the current time. Otherwise, time 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

# 显示文件的 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 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!!

Python File(文件) 方法Python OS File/Directory Methods

Other Extensions