How to get file creation and modification date/times in python?

How to get file creation and modification date/times in python?

You can obtain file creation and modification date/times in Python using the os.path.getctime() and os.path.getmtime() functions from the os module. These functions return timestamps representing the creation and modification times of a file, respectively. You can then convert these timestamps into more human-readable formats using the datetime module.

Here's how to do it:

import os import datetime file_path = 'example.txt' # Replace with the path to your file # Get file creation time creation_timestamp = os.path.getctime(file_path) creation_datetime = datetime.datetime.fromtimestamp(creation_timestamp) print(f"File creation time: {creation_datetime}") # Get file modification time modification_timestamp = os.path.getmtime(file_path) modification_datetime = datetime.datetime.fromtimestamp(modification_timestamp) print(f"File modification time: {modification_datetime}") 

In this example:

  1. We import the necessary modules, os and datetime.

  2. You should specify the file_path variable with the path to the file for which you want to obtain creation and modification times.

  3. We use os.path.getctime(file_path) to get the creation timestamp and os.path.getmtime(file_path) to get the modification timestamp.

  4. We convert the timestamps into human-readable datetime objects using datetime.datetime.fromtimestamp(). This step allows you to format and display the creation and modification times in a more user-friendly way.

After running this code, you'll see the creation and modification times of the specified file in a human-readable format.

Examples

  1. Python: Get file creation date/time using os.path.getctime():

    Description: This code snippet demonstrates how to retrieve the creation date and time of a file in Python using the os.path.getctime() function.

    import os.path import datetime # Specify the file path file_path = 'example.txt' # Get file creation timestamp creation_timestamp = os.path.getctime(file_path) # Convert timestamp to datetime object creation_datetime = datetime.datetime.fromtimestamp(creation_timestamp) print("File creation date/time:", creation_datetime) 
  2. Python: Get file modification date/time using os.path.getmtime():

    Description: This code illustrates how to obtain the modification date and time of a file in Python using the os.path.getmtime() function.

    import os.path import datetime # Specify the file path file_path = 'example.txt' # Get file modification timestamp modification_timestamp = os.path.getmtime(file_path) # Convert timestamp to datetime object modification_datetime = datetime.datetime.fromtimestamp(modification_timestamp) print("File modification date/time:", modification_datetime) 
  3. Python: Retrieve file creation and modification date/times using os.stat():

    Description: This code snippet demonstrates how to obtain both the creation and modification date/times of a file in Python using the os.stat() function.

    import os import datetime # Specify the file path file_path = 'example.txt' # Get file stats file_stats = os.stat(file_path) # Extract creation and modification timestamps creation_timestamp = file_stats.st_ctime modification_timestamp = file_stats.st_mtime # Convert timestamps to datetime objects creation_datetime = datetime.datetime.fromtimestamp(creation_timestamp) modification_datetime = datetime.datetime.fromtimestamp(modification_timestamp) print("File creation date/time:", creation_datetime) print("File modification date/time:", modification_datetime) 
  4. Python: Get file creation and modification date/times using pathlib.Path():

    Description: This code showcases how to retrieve both the creation and modification date/times of a file in Python using the pathlib.Path() class.

    from pathlib import Path import datetime # Specify the file path file_path = Path('example.txt') # Get file creation and modification date/times creation_datetime = datetime.datetime.fromtimestamp(file_path.stat().st_ctime) modification_datetime = datetime.datetime.fromtimestamp(file_path.stat().st_mtime) print("File creation date/time:", creation_datetime) print("File modification date/time:", modification_datetime) 
  5. Python: Retrieve file creation and modification date/times using os.path.getctime() and os.path.getmtime():

    Description: This code snippet demonstrates how to use both os.path.getctime() and os.path.getmtime() functions to obtain file creation and modification date/times in Python.

    import os.path import datetime # Specify the file path file_path = 'example.txt' # Get file creation and modification timestamps creation_timestamp = os.path.getctime(file_path) modification_timestamp = os.path.getmtime(file_path) # Convert timestamps to datetime objects creation_datetime = datetime.datetime.fromtimestamp(creation_timestamp) modification_datetime = datetime.datetime.fromtimestamp(modification_timestamp) print("File creation date/time:", creation_datetime) print("File modification date/time:", modification_datetime) 
  6. Python: Get file creation and modification date/times using os.path.getatime():

    Description: This code illustrates how to retrieve the access, modification, and creation date/times of a file in Python using the os.path.getatime() function.

    import os.path import datetime # Specify the file path file_path = 'example.txt' # Get file access, modification, and creation timestamps access_timestamp = os.path.getatime(file_path) modification_timestamp = os.path.getmtime(file_path) creation_timestamp = os.path.getctime(file_path) # Convert timestamps to datetime objects access_datetime = datetime.datetime.fromtimestamp(access_timestamp) modification_datetime = datetime.datetime.fromtimestamp(modification_timestamp) creation_datetime = datetime.datetime.fromtimestamp(creation_timestamp) print("File access date/time:", access_datetime) print("File modification date/time:", modification_datetime) print("File creation date/time:", creation_datetime) 
  7. Python: Retrieve file creation and modification date/times using os.stat_result object:

    Description: This code demonstrates how to utilize the os.stat_result object returned by os.stat() to obtain file creation and modification date/times in Python.

    import os import datetime # Specify the file path file_path = 'example.txt' # Get file stats file_stats = os.stat(file_path) # Extract creation and modification timestamps from os.stat_result object creation_timestamp = file_stats.st_ctime modification_timestamp = file_stats.st_mtime # Convert timestamps to datetime objects creation_datetime = datetime.datetime.fromtimestamp(creation_timestamp) modification_datetime = datetime.datetime.fromtimestamp(modification_timestamp) print("File creation date/time:", creation_datetime) print("File modification date/time:", modification_datetime) 
  8. Python: Get file creation and modification date/times using os.path.getctime() and os.path.getmtime() with pathlib.Path():

    Description: This code snippet demonstrates how to use both os.path.getctime() and os.path.getmtime() functions along with pathlib.Path() to retrieve file creation and modification date/times.

    import os.path from pathlib import Path import datetime # Specify the file path file_path = Path('example.txt') # Get file creation and modification timestamps creation_timestamp = os.path.getctime(file_path) modification_timestamp = os.path.getmtime(file_path) # Convert timestamps to datetime objects creation_datetime = datetime.datetime.fromtimestamp(creation_timestamp) modification_datetime = datetime.datetime.fromtimestamp(modification_timestamp) print("File creation date/time:", creation_datetime) print("File modification date/time:", modification_datetime) 
  9. Python: Retrieve file creation and modification date/times using os.path.getctime() and os.path.getmtime() with os.path.join():

    Description: This code showcases how to use both os.path.getctime() and os.path.getmtime() functions with os.path.join() to obtain file creation and modification date/times.

    import os.path import datetime # Specify the file path file_name = 'example.txt' directory = '/path/to/directory' # Get file creation and modification timestamps file_path = os.path.join(directory, file_name) creation_timestamp = os.path.getctime(file_path) modification_timestamp = os.path.getmtime(file_path) # Convert timestamps to datetime objects creation_datetime = datetime.datetime.fromtimestamp(creation_timestamp) modification_datetime = datetime.datetime.fromtimestamp(modification_timestamp) print("File creation date/time:", creation_datetime) print("File modification date/time:", modification_datetime) 
  10. Python: Get file creation and modification date/times using os.path.getctime() and os.path.getmtime() with os.path.abspath():

    Description: This code demonstrates how to use both os.path.getctime() and os.path.getmtime() functions with os.path.abspath() to retrieve file creation and modification date/times.

    import os.path import datetime # Specify the file path file_path = '/path/to/directory/example.txt' # Get file creation and modification timestamps creation_timestamp = os.path.getctime(file_path) modification_timestamp = os.path.getmtime(file_path) # Convert timestamps to datetime objects creation_datetime = datetime.datetime.fromtimestamp(creation_timestamp) modification_datetime = datetime.datetime.fromtimestamp(modification_timestamp) print("File creation date/time:", creation_datetime) print("File modification date/time:", modification_datetime) 

More Tags

amazon-elb vb.net-to-c# cllocationmanager instanceof azure-logic-apps mongoengine filestream gesturedetector angular2-compiler circular-dependency

More Python Questions

More Auto Calculators

More Cat Calculators

More Livestock Calculators

More Biology Calculators