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
#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


Messages In This Thread
RE: How to acquire an imported file creation time - by Larz60+ - Sep-22-2021, 09:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can a module tell where it is being imported from? stevendaprano 3 1,218 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,863 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  module detecting if imported vs not Skaperen 1 1,704 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  Check last time file was accessed Pavel_47 4 2,879 Jun-01-2021, 05:47 PM
Last Post: Yoriz
  [newbie] Why is a module imported twice? Winfried 3 4,113 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,249 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Naming the file as time and date. BettyTurnips 3 3,037 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,138 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,538 Dec-08-2020, 10:11 PM
Last Post: greenpine
  Getting Correct Creation Time JohnVogel 14 6,708 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