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
# 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 |
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,
# 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 |
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
# 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 |
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