[Python] Editing "Date Modified" file attribute

Python function used to edit the "Date Modified" file attrib

Page 1 of 1

0 Replies - 3155 Views - Last Post: 08 February 2009 - 05:46 PM Rate Topic: -----

#4 dwayne   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 02-August 08

Re: [Python] Editing "Date Modified" file attribute

Posted 08 February 2009 - 05:46 PM

I am working on a project that needs to edit the "date modified" attribute of a file. I do not have any code since I cannot work on the project until I know what function to use. I am NOT asking for code in return since I have supplied no code. Also, what modules (if any) would I need to use?


I have resolved my issue after some time on various sites and tutorials on time handling in Python, as well as much trial and error. This method should be Windows and Unix compatible, unlike all the tutorials I found for doing this (they involved importing Win32 modules and too much junk).

Here is my testing code:
import os, time

#path to file
file = 'C:\\test\\time.txt'

#get current file stats
stats = os.stat(file)

#convert time to a more useable format
lastmod_date = time.localtime(stats[8])

#print the last date of modification

#new value to be stored in "Date Modified" file attribute
modDate = (2009, 2, 4, 20, 20, 12, 6, 39, 0)

#print new value (to compare against modified file

#convert time to a value the system can handle
SysTime = time.mktime(modDate)

#print the value after conversion

#update the file attributes
os.utime('C:\\test\\time.txt', (int(time.time()), SysTime))

Yes, I know some of the variable names are a bit odd and should be changed, but it got the job done for testing.

I may do some more work on this such as refining the code, making it accept user input, and possibly adding a quick GUI and then toss it all in a tutorial.

This post has been edited by dwayne: 08 February 2009 - 07:09 PM

Was This Post Helpful? 1

Page 1 of 1