Python Forum
How to acquire an imported file creation time - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to acquire an imported file creation time (/thread-34992.html)



How to acquire an imported file creation time - thunderspeed - Sep-22-2021

Hello everyone,

I have a number of pickle files and I wonder how can I get the creation date and time in python for every imported file.

Thanks in advance


RE: How to acquire an imported file creation time - Larz60+ - Sep-22-2021

The following will return dates on files.
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



RE: How to acquire an imported file creation time - thunderspeed - Sep-22-2021

Thank you for your reply

I get this error: 'str' object has no attribute 'state'


RE: How to acquire an imported file creation time - ibreeden - Sep-23-2021

I see no "state" in the code. Do you mean "stat"? Please show the complete error stack trace (in [error] tags).


RE: How to acquire an imported file creation time - Larz60+ - Sep-23-2021

Also make sure you aree using pathlib (posix) paths