Python can be used for getting time details about when was some file created or modified last time. But question which came to my mind when I came to know about TimeStamps and How os module can be used for accessing these was – Why programmer need to know TimeStamp of a file? It’s because sometimes it is important to know when file is created, like if you create a website using Python’s Django Framework then whenever a page is updated modification date should be added. As this will enable Google to identify that webpage has changed, consequently Google will update its index.
This is one of scenario where TimeStamps can be helpful. Let’s see How to get, set file timestamps using Python?
Getting File TimeStamp Using Python
Python’s os module function os.stat() can be used for getting information about a file, this function returns an os.statresult object from which TimeStamp of file can be extracted.
Returned object os.statresult from function call os.stat() contains properties –
st_atime => Last File Access Time
st_mtime => Last File Modification Time
st_ctime => File Creation Time
Below is a code example showing How to get File TimeStamp Using Python?
import os stats = os.stat('somefile') print(stats.st_atime) # Prints out 1615950895.8777606 print(stats.st_mtime) # Prints out 1615950893.747034 print(stats.st_ctime) # Prints out 1615950894.7286537
But wait what these print(stats.st_atime), print(stats.st_mtime), print(stats.st_ctime) are some weard numbers 😮 😮 😮 these are neither time nor date.
Well actually these are time, date but not formatted properly. These numbers can be formatted properly using Python’s datetime module.
Below is a code showing formating of stats.st_atime, stats.st_mtime, stats.st_ctime.
import os import datetime stats = os.stat('somefile') print(stats.st_atime) # Prints out 1615950895.8777606 print(stats.st_mtime) # Prints out 1615950893.747034 print(stats.st_ctime) # Prints out 1615950894.7286537 date_object_st_atime = datetime.datetime.fromtimestamp(stats.st_ctime) date_object_st_mtime = datetime.datetime.fromtimestamp(stats.st_mtime) date_object_st_ctime = datetime.datetime.fromtimestamp(stats.st_ctime) print("Last time file was accesses => ", date_object_st_atime) print("Last time file was Modified => ", date_object_st_mtime) print("File was created => ", date_object_st_ctime) # Above Code Prints out Last time file was accesses => 2021-03-19 14:13:19.034281 Last time file was Modified => 2021-03-19 14:13:18.986962 File was created => 2021-03-19 14:13:19.034281
Time outputted by above code is Human Readable.
Setting TimeStamp of a File Using Python
Python’s os module function utime() can be used for setting last modification, access time of a file.
Below is a code example showing How to set TimeStamp of a File.
import os import datetime # To specify the timestamp to set, in seconds as tuple: # (access_time, modify_time) time_in_seconds = datetime.datetime.now().timestamp() os.utime('somefile', times=(time_in_seconds, time_in_seconds))