By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

How can time be compared

P: 9

I am writing a script that is going to download and read XML data from a server. The server updates the xml data every 4 hours. Instead of downloading a new copy of the xml everytime the script runs, I want to look at the timestamp of the local file to see if it is less than 4 hours old. If it is, I will read the local copy, but if not I will download a new copy and read it. I can't seem to find any good way of doing this. Any help would be appreciated.
Sep 8 '10 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 621
Try os.stat() and use either ctime, mtime, or stime, depending on your requirements Note that all times must be in the same units, seconds since the epoch, or datetime format.
Sep 8 '10 #2

Expert Mod 2.5K+
P: 2,851
Here's a simple example using os.path.getmtime and time.time:
Expand|Select|Wrap|Line Numbers
  1. import time, os
  3. def mtime_diff(fn):
  4.     '''
  5.     Return the difference between the current time and the time of
  6.     last modification of a given file name in seconds.'''
  7.     return time.time() - os.path.getmtime(fn)
  9. diff = mtime_diff('file_name')
  10. fourhours = 60*60*4.
  11. print "The file was last modified %.2f hours ago." % (diff/fourhours)
Sep 8 '10 #3

P: 9
thank you sir. That did the trick!
Sep 9 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.