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

How to get file modification time in Python

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.

  1. 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}")
    
  2. 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}")
    
  3. 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}")
    
  4. 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}")
    
  5. 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}")
    
  6. 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')}")
    
  7. 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}")
    
  8. 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)}")
    
  9. 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}")
    
  10. 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}")
    
  11. 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}")
    
  12. 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)}")
    
  13. 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}")