Python | os.utime() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.utime() method of os module in Python is used to set the access and modified time of the specified path.

Syntax: os.utime(path, times = None, *, [ns, ]dir_fd = None, follow_symlinks = True)

Parameter:
path: A string or bytes object representing a valid file system path.
times (optional): A 2-tuple of the form (atime, mtime) where each member is an integer or float value representing access time and modification time in seconds respectively.
ns (optional): A 2-tuple of the form (atime_ns, mtime_ns) where each member is an integer or float value representing access time and modification time in nanoseconds respectively.
dir_fd: A file descriptor referring to a directory. The default value of this parameter is None.
follow_symlinks: A boolean value either True or False. If True method will follow symbolic link otherwise not.

Return Type: This method does not return any value



Code #1: Use of os.utime() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.utime() method 
    
# importing os module 
import os
  
# Path
path = '/home / ihritik / Documents / file.txt'
  
# Print current access and modification time
# of the above specified path
print("Current access time:", os.stat(path).st_atime)
print("Current modification time:", os.stat(path).st_mtime)
  
# Access time in seconds
atime = 200000000
  
# Modification time in seconds
mtime = 100000000
  
# Set the access time and 
# modification time for the
# above specified path
# using os.utime() method
tup = (atime, mtime)
os.utime(path, tup)
  
print("\nAccess and modification time changed\n")
  
# Print current access and modification time
print("Current access time:", os.stat(path).st_atime)
print("Current modification time:", os.stat(path).st_mtime)
  
# Either we can specify times
# or specify ns parameter.
# It is an error to specify
# tuples for both times and ns

chevron_right


Output:

Current access time (in seconds): 1568930018.710342
Current modification time (in seconds): 1568930018.610892

Access and modification time changed

Current access time (in seconds): 200000000.0
Current modification time (in seconds): 100000000.0

Code #2: If ns parameter is specified,

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.utime() method 
    
# importing os module 
import os
  
  
# Path
path = '/home / ihritik / Documents / file.txt'
  
  
# Print current access and modification time
# of the above specified path
print("Current access time (in seconds):", os.stat(path).st_atime)
print("Current modification time (in seconds):", os.stat(path).st_mtime)
  
# Access time in nanoseconds
atime_ns = 20000000012345
  
# Modification time in nanoseconds
mtime_ns = 10000000012345
  
# Set the access time and 
# modification time in nanoseconds 
# for the above specified path
# using os.utime() method
# (ns is keyword only argument) 
tup = (atime_ns, mtime_ns)
os.utime(path, ns = tup)
  
print("\nAccess and modification time changed\n")
  
# Print current access and modification time
print("Current access time (in seconds):", os.stat(path).st_atime)
print("Current modification time (in seconds):", os.stat(path).st_mtime)
  
# Either we can specify times
# or specify ns parameter.
# It is an error to specify
# tuples for both times and ns

chevron_right


Output:

Current access time (in seconds): 1568930018.710342
Current modification time (in seconds): 1568930018.610892

Access and modification time changed

Current access time (in seconds): 20000.000012345
Current modification time (in seconds): 10000.000012345

Code #3: If times parameter is None and ns parameter is unspecified

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.utime() method 
    
# importing os module 
import os
  
  
# Path
path = '/home / ihritik / Documents / file.txt'
  
  
# Print current access and modification time
# of the above specified path
print("Current access time (in seconds):", os.stat(path).st_atime)
print("Current modification time (in seconds):", os.stat(path).st_mtime)
  
  
# Set the access time and 
# modification time in nanoseconds 
# for the above specified path
# using os.utime() method
os.utime(path)
  
print("\nAccess and modification time changed\n")
  
# Print current access and modification time
print("Current access time (in seconds):", os.stat(path).st_atime)
print("Current modification time (in seconds):", os.stat(path).st_mtime)
  
# If times is None and ns is unspecified,
# then it will be equivalent to
# specifying ns = (atime_ns, mtime_ns)
# where member atime_ns and mtime_ns
# are current time in nanoseconds 

chevron_right


Output:

Current access time (in seconds): 20000.000012345
Current modification time (in seconds): 10000.000012345

Access and modification time changed

Current access time (in seconds): 1568930018.710342
Current modification time (in seconds): 1568930018.610892

Reference: https://docs.python.org/3/library/os.html#os.utime




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.