Python Os.utime() Method



Python method utime() sets the access and modified times of the file specified by path.


Following is the syntax for utime() method:

os.utime(path, times)


  • path − This is the path of the file.
  • times − This is the file access and modified time. If times are none, then the file access and modified times are set to the current time. The parameter times consists of the row in the form of (atime, mtime) i.e (accesstime, modifiedtime).

Return Value

This method does not return any value.


The following example shows the usage of utime() method.

# !/usr/bin/python

import os, sys

# Showing stat information of file
stinfo = os.stat('a2.py')
print stinfo

# Using os.stat to recieve atime and mtime of file
print "access time of a2.py: %s" %stinfo.st_atime
print "modified time of a2.py: %s" %stinfo.st_mtime

# Modifying atime and mtime
os.utime("a2.py",(1330712280, 1330712292))
print "done!!"

When we run the above program, it produces the following result:

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

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.