Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
To get the file modification time in Python, you can use the os.path.getmtime()
function from the os.path
module, which is part of the Python standard library. This function returns the file modification time as a float, representing the number of seconds since the epoch (January 1, 1970, 00:00:00 UTC).
Here's an example:
import os.path # The file path file_path = "example.txt" # Get the file modification time (in seconds since the epoch) modification_time = os.path.getmtime(file_path) # Print the file modification time print("The file modification time is:", modification_time)
In this example, we have a file path called file_path
. We use the os.path.getmtime()
function to get the file modification time as a float representing the number of seconds since the epoch. Then, we print the file modification time.
If you want to convert the modification time to a datetime
object or a human-readable string, you can use the datetime.datetime.fromtimestamp()
function from the datetime
module:
import os.path import datetime # The file path file_path = "example.txt" # Get the file modification time (in seconds since the epoch) modification_time = os.path.getmtime(file_path) # Convert the modification time to a datetime object modification_datetime = datetime.datetime.fromtimestamp(modification_time) # Print the file modification time as a datetime object print("The file modification time is:", modification_datetime) # Print the file modification time as a human-readable string print("The file modification time is:", modification_datetime.strftime("%Y-%m-%d %H:%M:%S"))
In this example, we use the datetime.datetime.fromtimestamp()
function to convert the file modification time to a datetime
object, and then we use the strftime()
method to format the modification time as a human-readable string.
Retrieve file modification time using os.path.getmtime() in Python:
import os import time file_path = 'example.txt' modification_time = os.path.getmtime(file_path) print(f"File modification time: {modification_time}")
Get last modified time of a file in Python:
import os from datetime import datetime file_path = 'example.txt' modification_time = os.path.getmtime(file_path) last_modified = datetime.fromtimestamp(modification_time) print(f"Last modified time: {last_modified}")
Using os.stat() for file modification time in Python:
import os from datetime import datetime file_path = 'example.txt' stat_info = os.stat(file_path) modification_time = stat_info.st_mtime last_modified = datetime.fromtimestamp(modification_time) print(f"Last modified time: {last_modified}")
Python os.path.getctime() for file creation time:
import os from datetime import datetime file_path = 'example.txt' creation_time = os.path.getctime(file_path) created_at = datetime.fromtimestamp(creation_time) print(f"File creation time: {created_at}")
Access file modification time with os.path.getatime() in Python:
import os from datetime import datetime file_path = 'example.txt' access_time = os.path.getatime(file_path) last_accessed = datetime.fromtimestamp(access_time) print(f"Last accessed time: {last_accessed}")
Get file modification time in a human-readable format in Python:
import os from datetime import datetime file_path = 'example.txt' modification_time = os.path.getmtime(file_path) last_modified = datetime.fromtimestamp(modification_time) print(f"Last modified time: {last_modified.strftime('%Y-%m-%d %H:%M:%S')}")
Finding file modification time using pathlib in Python:
from pathlib import Path import time file_path = Path('example.txt') modification_time = file_path.stat().st_mtime print(f"File modification time: {modification_time}")
Retrieve file modification time with os.path.getmtime() and localtime():
import os import time file_path = 'example.txt' modification_time = os.path.getmtime(file_path) last_modified = time.localtime(modification_time) print(f"Last modified time: {time.strftime('%Y-%m-%d %H:%M:%S', last_modified)}")
Getting file modification time with os.path.getmtime() and time.ctime() in Python:
import os import time file_path = 'example.txt' modification_time = os.path.getmtime(file_path) last_modified = time.ctime(modification_time) print(f"Last modified time: {last_modified}")
Accessing file modification time using os.path.getmtime() and arrow library in Python:
import os import arrow file_path = 'example.txt' modification_time = os.path.getmtime(file_path) last_modified = arrow.get(modification_time) print(f"Last modified time: {last_modified}")
Finding file modification time with os.path.getmtime() and pendulum library in Python:
import os import pendulum file_path = 'example.txt' modification_time = os.path.getmtime(file_path) last_modified = pendulum.from_timestamp(modification_time) print(f"Last modified time: {last_modified}")
Get file modification time with os.path.getmtime() and time.gmtime() in Python:
import os import time file_path = 'example.txt' modification_time = os.path.getmtime(file_path) last_modified_utc = time.gmtime(modification_time) print(f"Last modified time (UTC): {time.strftime('%Y-%m-%d %H:%M:%S', last_modified_utc)}")
Accessing file modification time using os.path.getmtime() and pandas in Python:
import os import pandas as pd file_path = 'example.txt' modification_time = os.path.getmtime(file_path) last_modified = pd.to_datetime(modification_time, unit='s') print(f"Last modified time: {last_modified}")