Skip to content

Instantly share code, notes, and snippets.

@Hiyorimi
Created December 28, 2018 23:32
Show Gist options
  • Save Hiyorimi/08c73d273400ac24a05e0107a85b6211 to your computer and use it in GitHub Desktop.
Save Hiyorimi/08c73d273400ac24a05e0107a85b6211 to your computer and use it in GitHub Desktop.
script to adjust access and modification time based on the filename
import os
import glob
import re
import datetime as dt
from stat import *
if __name__ == '__main__':
regexp = re.compile("(\d{4}\d{2}\d{2}_\d{2}\d{2}\d{2})")
for file in glob.iglob("*.jpg"):
timestring = regexp.search(file).group(0)
datetime_object = dt.datetime.strptime( timestring, '%Y%m%d_%H%M%S')
st = os.stat(file)
atime = st[ST_ATIME] #access time
mtime = st[ST_MTIME] #modification time
new_mtime = int(datetime_object.strftime('%s')) #new modification time
new_atime = new_mtime
#modify the file timestamp
os.utime(file,(new_atime,new_mtime))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment