Skip to content

Instantly share code, notes, and snippets.

@mrdoob
Created August 1, 2011 11:12
Show Gist options
  • Save mrdoob/1117948 to your computer and use it in GitHub Desktop.
Save mrdoob/1117948 to your computer and use it in GitHub Desktop.
Fixes timestamp of image files in a folder (using filename data)
import time
import datetime
from stat import *
#returns a list of all the files on the current directory
files = os.listdir('.')
for f in files:
#my folder has some jpegs and raw images
if f.lower().endswith('jpg'):
st = os.stat(f)
atime = st[ST_ATIME] #access time
mtime = st[ST_MTIME] #modification time
#new_mtime = mtime + (4*3600) #new modification time
print '----'
print f
print mtime
if f.find('IMG') != -1:
year = f[4:8]
month = f[8:10]
day = f[10:12]
hour = f[13:15]
minute = f[15:17]
second = f[17:19]
else:
year = f[0:4]
month = f[5:7]
day = f[8:10]
hour = f[11:13]
minute = f[14:16]
second = f[17:19]
#print year + ' ' + month + ' ' + day + ' ' + hour + ' ' + minute + ' ' + second
#print datetime.datetime(int(year), int(month), int(day), int(hour), int(minute), int(second))
mtime = int(time.mktime(datetime.datetime(int(year), int(month), int(day), int(hour), int(minute), int(second)).timetuple()))
print mtime
#modify the file timestamp
os.utime(f,(atime,mtime))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment