Skip to content

Instantly share code, notes, and snippets.

@stevekm
Created January 3, 2017 03:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevekm/4e53b3653a73d62367bd1f0070da20bb to your computer and use it in GitHub Desktop.
Save stevekm/4e53b3653a73d62367bd1f0070da20bb to your computer and use it in GitHub Desktop.
return ISO compliant timestamp from file modification time datetime
>>> import os
>>> import datetime
>>> file = "my_file.txt"
>>> os.stat(file).st_ctime
1483323862.8774118
>>> os.path.getmtime(file)
1483323862.873167
>>> os.path.getmtime(os.path.getmtime(file))
KeyboardInterrupt
>>> datetime.datetime.fromtimestamp(os.path.getmtime(file))
datetime.datetime(2017, 1, 1, 21, 24, 22, 873167)
>>> datetime.datetime.fromtimestamp(os.path.getmtime(file)).strftime("%Y")
'2017'
>>> datetime.datetime.fromtimestamp(os.path.getmtime(file)).isoformat()
'2017-01-01T21:24:22.873167'
>>> quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment