Mismatch between file creation time and current time in Python

Viewed 97

Why is the file creation time less than the time measured before it is created?

import os, time

before_creation = time.time()

with open('my_file', 'w') as f:
    f.write('something')

creation_time = os.stat('my_file').st_ctime

print(before_creation)  # 1545031532.8819697
print(creation_time)    # 1545031532.8798425
print(before_creation < creation_time)  # False

Edit OS is Linux

1 Answers
  • Since OS module is from CPython and designed initally for the python 2.x versions. If you run your code in 2.7 version it returns the same value for before_creation and creation_time. Since, the result is limited to 2 decimal places only in 2.x version.
    eg., 1545073155.03
  • Also, you have to note that when you do print(os.stat('my_file')) we get
    posix.stat_result(st_mode=33204, st_ino=12, st_dev=1792, st_nlink=1, st_uid=488, st_gid=487, st_size=0, st_atime=1545073155, st_mtime=1545073155, st_ctime=1545073155)
  • And if you do print(os.stat('my_file')[9]) we get 1545073155
    As far as solution, you might have to use the time.time() to get the time after creation.
    Considering this there might be a bug during the conversion from int to float of the st_ctime.
Related