Sep-22-2021, 09:13 PM
The following will return dates on files.
Need to pass full pathlib filename (or just filename if in same directory as script)
Need to pass full pathlib filename (or just filename if in same directory as script)
import time import datetime from pathlib import Path import os def get_file_dates(filename): last_mod_time = datetime.datetime.fromtimestamp(filename.stat().st_mtime) create_date = datetime.datetime.fromtimestamp(filename.stat().st_ctime) return last_mod_time, create_date if __name__ == '__main__': # Set current directory = script directory (not necessary if full path used). os.chdir(os.path.abspath(os.path.dirname(__file__))) filename = Path("newin.xlsx") moddate, createdate = get_file_dates(filename) print(f"File {filename} was created on {createdate}, and modified on {moddate}")example:
python CheckFileDates.py
Output:File newin.xlsx was created on 2021-09-07 10:44:36.218143, and modified on 2021-09-07 10:44:36.218143