Python Forum
How to acquire an imported file creation time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to acquire an imported file creation time
#1
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
Reply
#2
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
Reply
#3
Thank you for your reply

I get this error: 'str' object has no attribute 'state'
Reply
#4
I see no "state" in the code. Do you mean "stat"? Please show the complete error stack trace (in [error] tags).
Reply
#5
Also make sure you aree using pathlib (posix) paths
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can a module tell where it is being imported from? stevendaprano 3 1,174 Apr-12-2022, 12:46 AM
Last Post: stevendaprano
  How to split file by same values from column from imported CSV file? Paqqno 5 2,775 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  module detecting if imported vs not Skaperen 1 1,670 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  Check last time file was accessed Pavel_47 4 2,825 Jun-01-2021, 05:47 PM
Last Post: Yoriz
  [newbie] Why is a module imported twice? Winfried 3 4,076 Apr-02-2021, 04:48 AM
Last Post: deanhystad
  find the header location in a .bin file without reading the whole file at a time SANJIB 0 2,209 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Naming the file as time and date. BettyTurnips 3 2,971 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  xml file creation from an XML file template and data from an excel file naji_python 1 2,106 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  how to change the range of read CSV file every time python file runs greenpine 6 4,461 Dec-08-2020, 10:11 PM
Last Post: greenpine
  Getting Correct Creation Time JohnVogel 14 6,505 Aug-17-2020, 03:33 PM
Last Post: JohnVogel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020