Python os.utime() Method

Python os.utime() Method

  1. Syntax of os.utime() Method
  2. Example 1: Use the os.utime() Method in Python
  3. Example 2: Use Nanoseconds in the os.utime() Method
  4. Example 3: If time Is None and ns Is Unspecified in the os.utime() Method

Python os.utime() method is an efficient way of finding the modified time and setting the access times of a specified path.

Syntax of os.utime() Method

os.utime(path, time, ns)

Parameters

path It is an address object of a file system path or a symlink. The object can either be an str or bytes.
time It is a tuple of two elements (access_time, modified_time). Each element is an integer/float value representing times in seconds.
ns It is a tuple of two elements (access_time, modified_time). Each element is an integer/float value representing times in nanoseconds.

Return

In the execution process, this method does not return any value.

Example 1: Use the os.utime() Method in Python

import os

import sys
fd = os.open( "File.txt", os.O_RDWR|os.O_CREAT )

path = 'C:/Users/786/Documents/File.txt'

stinfo = os.stat(path)

print (stinfo)

print ("The access time of the file: %s" %stinfo.st_atime)

print ("The modified time of the file: %s" %stinfo.st_mtime)

os.utime(path,(1330712280, 1330712292))

print("Changes have been made.")

Output:

os.stat_result(st_mode=33206, st_ino=15481123719133579, st_dev=347648946, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1659182601, st_mtime=1659182601, st_ctime=1659182601)
The access time of the file: 1659182601.0513785
The modified time of the file: 1659182601.0513785
Changes have been made.

When using the os.utime() method, ensure you either enter the time parameter or the ns parameter. It is an error to set a value for both the parameters.

Example 2: Use Nanoseconds in the os.utime() Method

import os

import sys

fd = os.open( "File.txt", os.O_RDWR|os.O_CREAT )

path = 'C:/Users/786/Documents/File.txt'

print("The current access time, in seconds:", os.stat(path).st_atime)

print("The current modification time, in seconds:", os.stat(path).st_mtime)

access_time_ns = 20000000012345

modified_time_ns = 10000000012345

tuples = (access_time_ns, modified_time_ns)

os.utime(path, ns = tuples)

print("The access and modification times have been changed.")

Output:

The current access time, in seconds: 20000.0000123
The current modification time, in seconds: 10000.0000123
The access and modification times have been changed.

In the above code, ns is a keyword for the argument of the tuple. No time in nanoseconds can be set without using the ns keyword.

Example 3: If time Is None and ns Is Unspecified in the os.utime() Method

import os

import sys

fd = os.open( "File.txt", os.O_RDWR|os.O_CREAT )

path = 'C:/Users/786/Documents/File.txt'

print("The current access time, in seconds:", os.stat(path).st_atime)

print("The current modification time, in seconds:", os.stat(path).st_mtime)

os.utime(path)

print("The access and modification times have been changed.")

print("The current access time, in seconds:", os.stat(path).st_atime)

print("The current modification time, in seconds:", os.stat(path).st_mtime)

Output:

The current access time, in seconds: 20000.0000123
The current modification time, in seconds: 10000.0000123
The access and modification times have been changed.
The current access time, in seconds: 1659631237.2886252
The current modification time, in seconds: 1659631237.2886252

In the above code, the time is None by default, and ns is unspecified. So by default, the os.utime() method will take the current time in nanoseconds as the parameters.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method