Ярлыки

среда, 20 марта 2013 г.

Python: your time to each program (Windows)

Script that write how many time each program used in Windows (by title of show window).


import sys

#module for work with windows (32bit)
import win32gui

txt=''
time1=time.time()
#times dictionary
appdict=dict()
while True:
#pause (it's influence to accuracy)
    time.sleep(5)
#title of the current window
    newtxt = win32gui.GetWindowText(win32gui.GetForegroundWindow())
#if window was changed than update time of beginning work with other window
    if txt!=newtxt:
        time2=time.time()
#add previous time to dict
        if appdict.get(txt)!=None:
            appdict[txt]=appdict[txt]+round(time2,0)-round(time1,0)
        else:
            appdict[txt]=round(time2,0)-round(time1,0)
        txt=newtxt
        time1=time.time()
    f=open('timerep.csv', 'w')
    csvwr = csv.writer(f, delimiter=';',lineterminator='\n')
#We write only window that summary time over 5 minutes (Other window is non-influential).
    for i in appdict.items():
        if i[1]>=300:
            csvwr.writerow(i)
    f.close()

Комментариев нет:

Отправить комментарий