Python fraction of seconds

Viewed 40919

What is the best way to handle portions of a second in Python? The datetime library is excellent, but as far as I can tell it cannot handle any unit less than a second.

4 Answers

A different, non mentioned approach which I like:

from datetime import datetime
from time import sleep

t0 = datetime.now()
sleep(3)
t1 = datetime.now()
tdelta = t1 - t0
print(tdelta.total_seconds())
# will print something near (but not exactly 3)
# 3.0067
Related