0

I was browsing the Python guide and some search machines for a few hours now, but I can't really find an answer to my question.

I am writing a switch where only certain files are chosen to be in the a file_list (list[]) when they are modified after a given date.

In my loop I do the following code to get its micro time:

file_time = os.path.getmtime(path + file_name)

This returns me a nice micro time, like this: 1342715246.0

Now I want to compare if that time is after a certain date-time I give up. So for testing purposes, I used 1790:01:01 00:00:00.

# Preset (outside the class/object)
start_time = '1970-01-01 00:00:00'
start_time = datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')
start_time = start_time.strftime("%Y-%m-%d %H:%M:%S")

# In my Method this check makes sure I only append files to the list
#   that are modified after the given date
if file_time > self.start_time:
    file_list.append(file_name)

Of course this does not work, haha :P. What I'm aiming for is to make a micro time format from a custom date. I can only find ClassMethods online that make micro time formats from the current date.

2 Answers2

2

Take a look at Python datetime to microtime. You need the following snippet:

def microtime(dt):
    time.mktime(dt.timetuple()) + dt.microsecond / 1000000.0 
Community
  • 1
  • 1
jfocht
  • 1,704
  • 1
  • 12
  • 16
  • Worked like a charm, thanks :D. I've tested it on the files that were created yesterday after 21:00 (09:00 PM) and it works superb. Now the final step is that I will add a buffer List that will mirror each few seconds (LIVE) if new files are to be processed. This way I want to minimize the reprocessing of files that are already processed in a LIVE looping session. Again; thanks so much ^^ –  Jul 20 '12 at 15:58
0

You can compare datetime.datetime objects by themselves. If you keep start_time as a datetime.datetime object and make file_time a datetime.datetime object, you can successfully do file_time > self.start_time

Take a look at the supported operators in the documentation

smont
  • 1,348
  • 1
  • 12
  • 20