Python os.utime() Method



The 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 is none, then the file access and modified times are set to the current time. The parameter times consists of 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('')
print stinfo

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

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

When we run above program, it produces 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 1330498070
modified time of 1330498074