time.ctime(os.path.getmtime phyton in datetime 1

time.ctime(os.path.getmtime phyton in datetime

x = time.strftime("%d.%m.%Y", time.gmtime(os.path.getmtime("{}".format("file.txt"))))

# getctime can be used instead of getmtime
# getctime = file creation date
# getmtime = last modified file date

#"file.txt" can also be a filepath if it is in a different directory

# time.strftime("%d.%m.%Y")
# changes the output date from time.gmtime(...) in a more readable form.
# %d = day , %m = month, %y = year 
# . in in my case the seperator but it can be anything, like /

Here is what the above code is Doing:
1. os.path.getmtime(“file.txt”)
– gets the last modified date of the file
2. time.gmtime(…)
– converts the date into a readable format
3. time.strftime(“%d.%m.%Y”, …)
– changes the output date from time.gmtime(…) in a more readable form.
– %d = day , %m = month, %y = year
– . in in my case the seperator but it can be anything, like /

Similar Posts