3 Replies - 2645 Views - Last Post: 18 January 2013 - 05:58 PM Rate Topic: -----

#1 farazamiruddin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 13

Change timestamp on file to current time?

Posted 18 January 2013 - 05:15 PM

How to you change the timestamp on a file to the current time? essentially just "touching" the file so the time is changed to the current time
Is This A Good Question/Topic? 0
  • +

Replies To: Change timestamp on file to current time?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Change timestamp on file to current time?

Posted 18 January 2013 - 05:47 PM

Take a look at the os.utime function:

Quote

os.utime(path, times)


Set the access and modified times of the file specified by path. If times is None, then the file’s access and modified times are set to the current time. (The effect is similar to running the Unix program touch on the path.)

This post has been edited by sepp2k: 18 January 2013 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,589
  • Joined: 12-December 12

Re: Change timestamp on file to current time?

Posted 18 January 2013 - 05:48 PM

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))

Was This Post Helpful? 0
  • +
  • -

#4 farazamiruddin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 13

Re: Change timestamp on file to current time?

Posted 18 January 2013 - 05:58 PM

Thank you all for the help, got it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1