YouTip LogoYouTip

Os Utime

# Python2.x Python os.utime() Method [![Image 3: Python File Methods](#) Python OS File/Directory Methods](#) * * * ### Overview The os.utime() method is used to set the last modification and access times of a file at a specified path. It is effective on Unix and Windows. ### Syntax The syntax for the **utime()** method is as follows: os.utime(path, times) ### Parameters * **path** -- The file path. * **times** -- If the time is None, the file's access and modification times are set to the current time. Otherwise, the time is a 2-tuple of numbers, (atime, mtime), used to set the access and modification times respectively. ### Return Value This method does not return a value. ### Example The following example demonstrates the use of the utime() method: #!/usr/bin/python# -*- coding: UTF-8 -*-import os, sys # Display the stat information of the file stinfo = os.stat('a2.py')print stinfo # Use os.stat to receive the file's access and modification timesprint "a2.py access time: %s" %stinfo.st_atime print "a2.py modification time: %s" %stinfo.st_mtime # Modify the access and modification times os.utime("a2.py",(1330712280, 1330712292))print "done!!" Executing the above program outputs 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=1330498074, st_ctime=1330498065) a2.py access time: 1330498070 a2.py modification time: 1330498074done!! [![Image 4: Python File Methods](#) Python OS File/Directory Methods](#)
← Os WriteOs Utime β†’