find the last accessed date of a csv file in python

To find the last accessed date of a CSV file in Python, you can use the os module to get the file's stat object, which contains information about the file including its timestamps. You can then use the datetime module to convert the timestamp to a human-readable format.

Here's an example code snippet:

main.py
import os
import datetime

filename = 'example.csv'

# Get the file's stat object
file_stats = os.stat(filename)

# Extract the last accessed timestamp from the stat object
last_accessed_timestamp = file_stats.st_atime_ns

# Convert the timestamp to a datetime object
last_accessed_datetime = datetime.datetime.fromtimestamp(last_accessed_timestamp / 1e9)

# Format the datetime object as a string
last_accessed_str = last_accessed_datetime.strftime('%Y-%m-%d %H:%M:%S')

# Print the last accessed date and time
print(f'{filename} was last accessed on {last_accessed_str}')
574 chars
20 lines

This code will print something like:

main.py
example.csv was last accessed on 2021-10-20 12:34:56
53 chars
2 lines

gistlibby LogSnag