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
paththe abs path to your folder?/home/hellbreak/Documents/test.txt?