0

I want to get only one creation timestamp of any file in a folder because they all were created at the same time. When I use this code below, it only shows the timestamp of the folder but not the file. There are many folder with many files and the folder were created newer dates.

c_time = os.path.getctime(path)
dt_c = datetime.datetime.fromtimestamp(c_time)
3
  • Is path the abs path to your folder? Commented Sep 16, 2022 at 8:27
  • @hellbreak yes, path is the main folder.. in the main folder are sub folders and in the sub folders are files. I want to get the creation timestamp of one of the files and list in CSV. If I use the code above I only get the creation timestamp of the sub folder and not the files Commented Sep 16, 2022 at 8:34
  • have you tried to set the path to your file? like /home/hellbreak/Documents/test.txt ? Commented Sep 16, 2022 at 8:47

2 Answers 2

2

os.path.getctime() method in Python is used to get the system’s ctime of the specified path I guess you did right. test.txt Is on my current folder

import os
import datetime
path = 'test.txt'
c_time = os.path.getctime('test.txt')
dt_c = datetime.datetime.fromtimestamp(c_time)
print(dt_c)

Output:

2022-09-16 10:28:29.922116

If you want to find when your file was last modified, you can use os.path.getmtime()

import os
import datetime
m_time = os.path.getmtime(path)
dt = datetime.datetime.fromtimestamp(m_time)
print('Modified on:', dt)

Output:

Modified on: 2022-09-16 10:38:58.934354

Edited to reflect the comments If you want to get the timestamp of all the files in a folder, you have to list all of them first. You can do it with the help of os.listdir() The do something like this

dir_list = os.listdir(yourpathhere)
for f in dir_list[1:]:
    print("filename {}".format(f))
    c_time = os.path.getctime(f)
    dt_c = datetime.datetime.fromtimestamp(c_time)
    print("Creation date {}".format(dt_c))
    m_time = os.path.getmtime(path)
    dt = datetime.datetime.fromtimestamp(m_time)
    print('Modified on {}'.format(dt))

Output:

2022-09-16 10:38:58.934354
Modified on: 2022-09-16 10:38:58.934354
filename test.txt
Creation date 2022-09-16 10:38:58.934354
Modified on 2022-09-16 10:38:58.934354
filename test_2.py
Creation date 2022-09-16 11:02:29.505548
Modified on 2022-09-16 10:38:58.934354
Sign up to request clarification or add additional context in comments.

4 Comments

Ive tried both and both show only the timestamp of the folder and not the files. the files in the folder has different names thats why I cant take a path name like test.txt
So I guess you should first get a list of the files in your dir and then get the timestamp of each of them
@Volcano, I edited the code above to reflect your comment.
@Volcano The code above is an example. If you need to process several folders with several files, you can adapt it by listing and looping through the folders you need. anyway, Can you share a little bit of your code? I guess it would help to understand your case better.
0
    import os
    import platform

from datetime import datetime, timezone

def creation_date(path_to_file):
    if platform.system() == 'Windows':
       return os.path.getctime(path_to_file)
    else:
       stat = os.stat(path_to_file)
       try:
          # return stat.st_birthtime
          return datetime.fromtimestamp(stat.st_birthtime, tz=timezone.utc)
       except AttributeError:           
          # return stat.st_mtime
          return datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc)


date_creation = creation_date('/home/henro/Documents/help/app/main.py')

print (date_creation)
output:
2022-09-16 08:44:18.115245+00:0

change the path_file to yours

2 Comments

thanks for your answer. Ive tried your code. it shows me the Unix timestamp from the folder but not the files inside the folder. Any ideas on how I can get the timestamp from one of the files in the folder?
if path_to_file is assign only to folder, so in that function you need to add code in to the folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.