uptime for Win XP?

Peter Hansen peter at engcorp.com
Sun Dec 12 09:18:20 EST 2004


Bengt Richter wrote:
>  >>> import os
>  >>> [x for x in os.popen('pstat') if 'uptime' in x.lower()]
>  ['Pstat version 0.3:  memory: 327080 kb  uptime:  4 15:44:16.696 \n']
> 
[...]

> There's got to be something leaner though.

I believe there is, though I can't guarantee this is a
valid approach:

 >>> import datetime
 >>> import os
 >>> def uptime():
...   t = os.stat('c:/pagefile.sys').st_mtime
...   td = datetime.datetime.now() - datetime.datetime.fromtimestamp(t)
...   return td
...
 >>> print uptime()
12 days, 20:21:17.491000

(matches results of Bengt's and Fredrik's two approaches
two within a minute or so)

-Peter



More information about the Python-list mailing list