School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
You're Browsing As A Guest! Register Now...
Become an Expert!

Join 353,769 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,867 people online right now.Registration is fast and FREE... Join Now!



[Python] Editing "Date Modified" file attribute

[Python] Editing "Date Modified" file attribute Python function used to edit the "Date Modified" file attrib Rate Topic: -----

#4 dwayne  Icon User is offline

  • D.I.C Head
  • PipPip

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


Dream Kudos: 0

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

Posted 08 February 2009 - 04: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?

UPDATE:

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
print(lastmod_date)

#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
print(modDate)

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

#print the value after conversion
print(SysTime)

#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 - 06:09 PM

Was This Post Helpful? 1



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month