In [1]:
import os
import pathlib
import datetime
import time
import platform
In [2]:
p = pathlib.Path('data/temp/test.txt')
In [3]:
p.write_text('test')
time.sleep(10)
p.write_text('update')
Out[3]:
In [4]:
print(p.stat())
In [5]:
print(type(p.stat()))
In [6]:
print(os.stat('data/temp/test.txt'))
In [7]:
print(type(os.stat('data/temp/test.txt')))
In [8]:
print(os.stat(p))
In [9]:
print(type(os.stat(p)))
In [10]:
print(p.stat() == os.stat('data/temp/test.txt') == os.stat(p))
In [11]:
st = p.stat()
In [12]:
print(st.st_atime)
In [13]:
print(st.st_mtime)
In [14]:
print(st.st_ctime)
In [15]:
print(st.st_birthtime)
In [16]:
print(type(st.st_ctime))
In [17]:
print(st.st_ctime_ns)
In [18]:
print(type(st.st_ctime_ns))
In [19]:
print(os.path.getatime('data/temp/test.txt'))
In [20]:
print(os.path.getmtime('data/temp/test.txt'))
In [21]:
print(os.path.getctime('data/temp/test.txt'))
In [22]:
print(os.path.getctime(p))
In [23]:
print(os.path.getctime(p) == p.stat().st_ctime)
In [24]:
dt = datetime.datetime.fromtimestamp(p.stat().st_ctime)
In [25]:
print(dt)
In [26]:
print(type(dt))
In [27]:
print(dt.strftime('%Y年%m月%d日 %H:%M:%S'))
In [28]:
print(dt.isoformat())
In [29]:
print(os.path.getmtime('data/temp/test.txt'))
In [30]:
print(p.stat().st_mtime)
In [31]:
print(datetime.datetime.fromtimestamp(p.stat().st_mtime))
In [32]:
def creation_date(path_to_file):
"""
Try to get the date that a file was created, falling back to when it was
last modified if that isn't possible.
See http://stackoverflow.com/a/39501288/1709587 for explanation.
"""
if platform.system() == 'Windows':
return os.path.getctime(path_to_file)
else:
stat = os.stat(path_to_file)
try:
return stat.st_birthtime
except AttributeError:
# We're probably on Linux. No easy way to get creation dates here,
# so we'll settle for when its content was last modified.
return stat.st_mtime
In [33]:
print(creation_date(p))
In [34]:
print(datetime.datetime.fromtimestamp(creation_date(p)))