Method os.utime() in Python 3

The function utime() is used to modify the access and modified times of a file specified by its path. In attempting to modify the datetime of the file using the ordinal format, it became apparent that this method was not accurate. It should be noted that this is not the same as converting datetime to be used in os.utime, as the format used in this solution requires integer values in the form of a Unix timestamp.


The function utime() can be used to modify both the access time and
modified time
s of a particular file indicated by its path.


The method syntax for utime() is as follows:

os.utime(path, times)


  • The given information specifies that the provided text is referring to a “path” which essentially denotes the location or directory of a particular file.

  • If the value of “times” is not specified, the file’s access and modified times will be set to the current time. The row’s parameter is in the form of (atime, mtime), which refers to the access time and modified time respectively. This is applicable to the <a class=”text-blue-600″ href=”” title=”Find the last time a file was accessed/used”>file access</a> and has been modified accordingly.

Return Value

This approach does not yield any outcome.


The method utime() is demonstrated in the following example.

# !/usr/bin/python3
import os, sys, time
# Showing stat information of file
stinfo = os.stat('foo.txt')
print (stinfo)
# Using os.stat to recieve atime and mtime of file
print ("access time of foo.txt: %s" %stinfo.st_atime)
print ("modified time of foo.txt: %s" %stinfo.st_mtime)
print (time.asctime( time.localtime(stinfo.st_atime)))
# Modifying atime and mtime
os.utime("foo.txt",(1330712280, 1330712292))
print ("after modification")
print (time.asctime( time.localtime(stinfo.st_atime)))
print ("done!!")


Upon executing the program mentioned above, the output obtained is as follows –

os.stat_result(st_mode = 33206, st_ino = 1688849860351098, 
   st_dev = 1017554828, st_nlink = 1, st_uid = 0, st_gid = 0, st_size = 0,
   st_atime = 1455684273, st_mtime = 1455684273, st_ctime = 1455683589)
access time of foo.txt: 1455684273.84375
modified time of foo.txt: 1455684273.84375
Wed Feb 17 10:14:33 2016
after modification
Fri Mar  2 23:48:00 2012


Frequently Asked Questions