Getting the current time in Python

Is there any way to get the current time off the system clock with Python? I’ve done several searches and have managed to get it to print the date and time as one string:

import datetime

which gives “2007-06-23 17:32:16.906000”. Is there a way of splitting that up so I can get the hours, minutes and seconds as separate numbers?

This is really a python only question, whilst its great people here are willing to help on a wide range of issues, you could probably google this one? Im sure there are online docs for this kinda stuff.


>>> import time
>>> time.localtime()
(2007, 6, 29, 14, 9, 57, 4, 180, 1)
>>> time.localtime()[1]

The output is: year, month, day, hour, minute, second, weekday number (Monday=0), day (by year), daylight savings time flag )

Thanks, now I see how to do it:D I didn’t know you could get it as a list, the documentation’s quite confusing:o

My pleasure. And yes, I sometimes find the Python docs to be confusing, or at least, not as descriptive as I’d like. So usually when I’m looking for a function I search the docs and when I find something that might work, I try it in the Python interpreter. For example, when looking this up, I googled for ‘python current time’. The first hit was I looked through there and found the localtime() method, but there was no description of the output. So I fired up the Python interpreter and tried it out.