We now have a youtube channel. Subscribe!

Python os.utime() Method with example



Hello dear readers! Welcome back to another section of our tutorial on Python. In this tutorial guide, we are going to be studying about the Os utime() method.

Python os utime() method sets the access and modified times of the file specified by path.

Syntax

The following below is the syntax for Python Os utime() method -

os.utime(path, times)


Parameter Details

  • path - This is the path of the file.
  • times - This is the file access and modified time. If times is none, then the file access and modified times are set to the current time.

Return Value

It does not return any value.

Example

The following below is a simple example -

# !/usr/bin/python

import os, sys

# Showing stat information of file
stinfo = os.stat('a2.py')
print stinfo

# Using os.stat to recieve atime and mtime of file
print "access time of a2.py: %s" %stinfo.st_atime
print "modified time of a2.py: %s" %stinfo.st_mtime

# Modifying atime and mtime
os.utime("a2.py",(1330712280, 1330712292))
print "done!!"

Output
When the above code is executed, it will produce 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=13
30498074, st_ctime=1330498065)
access time of a2.py: 1330498070
modified time of a2.py: 1330498074
done!!


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the Python OS walk() Method.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain