Skip to content

Instantly share code, notes, and snippets.

@twaddington
Created January 2, 2017 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twaddington/8b4077c7197d2129ffa17ff9842a6312 to your computer and use it in GitHub Desktop.
Save twaddington/8b4077c7197d2129ffa17ff9842a6312 to your computer and use it in GitHub Desktop.
Script to update file modification and access times. Unfortunately, does not update file creation time.
#!/usr/bin/python
import sys
import os
import time
from datetime import datetime
def modtime(path):
print path
if not os.path.isfile(path):
print "\tFile doesn't exist"
return
# Get the file's current mtime
mtime = os.stat(path).st_mtime
# Parse the mtime into a datetime instance
dt = datetime.fromtimestamp(os.stat(path).st_mtime)
# Mutate the datetime
ndt = dt.replace(year=dt.year - 1)
# Convert the datetime to a timestamp that works with utime
timestamp = time.mktime(ndt.timetuple())
# Update the file with the new timestamp
os.utime(path, (timestamp, timestamp))
print "\tOld: %s" % dt
print "\tNew: %s" % ndt
def main():
"""
Usage:
./modtime.py /Dropbox/Photos/sony/100MSDCF/DSC0{1633..1872}.JPG
"""
paths = sys.argv[1:]
for p in paths:
modtime(p)
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment