How to update file timestamps in Python
Sometimes you can be real picky like me about timestamps of files, for example, during my wedding we had a few digital cameras, and one of the cameras had its internal clock 4 hours behind. So what better way for a lazy guy like you to change timestamps than writing a short python script to fix the problem.

I wrote this script on the same folder where the pictures with the lagged modification times existed:
import os
import time
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') or f.lower().endswith('crw'):
st = os.stat(f)
atime = st[ST_ATIME] #access time
mtime = st[ST_MTIME] #modification time
new_mtime = mtime + (4*3600) #new modification time
#modify the file timestamp
os.utime(f,(atime,new_mtime))
Very nice, I like













January 29th, 2012 at 1:38 pm
Hi!
I am new to Python coding, and I trying to return the time stamps of all files in a certain folder. I was hoping to export these results (filename with corresponding saved time) to excel. Can you please help!!!
February 7th, 2012 at 11:19 am
Here:
>>> for file in os.listdir(‘.’):
… print os.stat(file)
…
posix.stat_result(st_mode=16873, st_ino=556850L, st_dev=234881026L, st_nlink=4, st_uid=501, st_gid=501, st_size=136L, st_atime=1319080694, st_mtime=1303602087, st_ctime=1303602087)
posix.stat_result(st_mode=16893, st_ino=67639515L, st_dev=234881026L, st_nlink=3, st_uid=501, st_gid=501, st_size=102L, st_atime=1328343414, st_mtime=1309571254, st_ctime=1309571254)
posix.stat_result(st_mode=16877, st_ino=556852L, st_dev=234881026L, st_nlink=14, st_uid=501, st_gid=501, st_size=476L, st_atime=1328343414, st_mtime=1323729831, st_ctime=1323729831)
posix.stat_result(st_mode=33152, st_ino=556901L, st_dev=234881026L, st_nlink=1, st_uid=501, st_gid=501, st_size=630L, st_atime=1326851190, st_mtime=1227840558, st_ctime=1299730310)
posix.stat_result(st_mode=33188, st_ino=556902L, st_dev=234881026L, st_nlink=1, st_uid=501, st_gid=501, st_size=43L, st_atime=1326851190, st_mtime=1227824842, st_ctime=1299730310)
posix.stat_result(st_mode=16877, st_ino=556824L, st_dev=234881026L, st_nlink=2, st_uid=501, st_gid=501, st_size=68L, st_atime=1328343414, st_mtime=1298518034, st_ctime=1299730268)
posix.stat_result(st_mode=33188, st_ino=556903L, st_dev=234881026L, st_nlink=1, st_uid=501, st_gid=501, st_size=176L, st_atime=1326851190, st_mtime=1289321134, st_ctime=1299730310)
Using os.stat(file_path) you will get all the timestamps about the file (access time, modification time,etc)
you can then use that output as you please, for example, writing on your excel file.