I wrote a program which (thanks to Windows after starting
folder) starts after logging in, write current time into log, then based on inputed name (as string) puts informations into the log.
It is made to fairly divide time I and my brother can be on computer (not important here) and I want to make it uncheateble by him (The log is current security).
If you choose to (go thru input()
) it will turn off or log you out of the computer right after it will write end time into the log.
At the same time it will write the current state (who has the time and how much of it) into cas.txt
(read time.txt
in english). This is easily accessible by him and me, thou sometimes I have to rewrite it based on the log, cause he "cheats".
The log file is called zaznam.txt
.
I want to upgrade it in two ways:
- I would like to detect which user is logged in (so no more name input needed => our name will be in the log with no exeption)
- When the Windows logg out or turn off button is pressed I would like to start a program to add the final pice into the log (so he can not strike off the program skipping name writing and then time calculating) (if that is possible in python) OR
- when the program is quited (by pressing the cross/ Alt+F4) it will log of the user (if that is possible)
My operating system is Windows 7
What the files looks like (You don't actually need to read this, just showing that I made some work, there is nothing to help you answer):
The cas.txt
(actual time stats, easily accessible)
Tonda 0:00:00
Janek 4 days, 12:01:20
it means:
my name, my spend time
his name, his spend time
One of the lines in the zaznam.txt
= log (he probably don't know about it or does not understand it) note: it is divided by tabs so it is not so easy to see it here :/.
2015-10-22 11:58:38.024605 Tonda 0:00:00;Janek 3 days, 2:19:31 Janek 2015-10-22 12:37:59.020734 Tonda 0:00:00;Janek 3 days, 2:58:51
>>>>>>>time started<<<<<<< >>>>>what "cas.txt" looked like<<<< >inputed name< >>>>>>>>time ended<<<<<<<< >>>what "cas.txt" now looks like<<<
And there is the program (I don't think it is clear for not czech-speaking person):
import datetime
import time
a = datetime.datetime.today()
###
zaznam = open("zaznam.txt", 'a')
zaznam.write("\n")
zaznam.write(str(a))
oldcas = open("cas.txt", "r").read().replace("\n", ";")
print(oldcas)
zaznam.write("\t" + oldcas)
zaznam.close()
###
jmeno = None
while (jmeno != "Tonda") and (jmeno != "Janek"):
jmeno = input("Zadej jméno (Janek/Tonda):\n")
###
zaznam = open("zaznam.txt", 'a')
zaznam.write("\t" + jmeno)
zaznam.close()
###
turnoff = input("Zmáčkni enter pro ukončení a něco před tím napiš, pokud chceš vypnout PC")
b = datetime.datetime.today()
###
zaznam = open("zaznam.txt", 'a')
zaznam.write("\t" + str(b))
zaznam.close()
###
soubor = open("C:/Users/Tonda_2/Documents/Python/Cas/cas.txt", "r")
radek1, radek2 = soubor.read().split("\n")
soubor.close()
Tday, Jday = 0, 0
if " day, " in radek1:
Tday, Tcas = radek1.split(" day, ")
Tonda, Tday = Tday.split()
Tday = int(Tday)
elif " days, " in radek1:
Tday, Tcas = radek1.split(" days, ")
Tonda, Tday = Tday.split()
Tday = int(Tday)
else:
Tonda, Tcas = radek1.split(" ")
if " day, " in radek2:
Jday, Jcas = radek2.split(" day, ")
Janek, Jday = Jday.split()
Jday = int(Jday)
elif " days, " in radek2:
Jday, Jcas = radek2.split(" days, ")
Janek, Jday = Jday.split()
Jday = int(Jday)
else:
Janek, Jcas = radek2.split(" ")
Th, Tm, Ts = Tcas.split(":")
Jh, Jm, Js = Jcas.split(":")
Th, Tm, Ts, Jh, Jm, Js = int(Th) + 24*Tday, int(Tm), int(Ts), int(Jh) + 24*Jday, int(Jm), int(Js)
Tuhrano = datetime.timedelta(hours = Th, minutes = Tm, seconds = Ts)
Juhrano = datetime.timedelta(hours = Jh, minutes = Jm, seconds = Js)
uhrano = b - a
if jmeno == "Tonda":
Tuhrano += uhrano
if jmeno == "Janek":
Juhrano += uhrano
Tday, Jday = 0, 0
if " day, " in str(Tuhrano):
Tday, Tuhrano = str(Tuhrano).split(" day, ")
Tday = int(Tday)
if " days, " in str(Tuhrano):
Tday, Tuhrano = str(Tuhrano).split(" days, ")
Tday = int(Tday)
if " day, " in str(Juhrano):
Jday, Juhrano = str(Juhrano).split(" day, ")
Jday = int(Jday)
if " days, " in str(Juhrano):
Jday, Juhrano = str(Juhrano).split(" days, ")
Jday = int(Jday)
print()
print(Tuhrano)
print(Juhrano)
print()
Th, Tm, Ts = str(Tuhrano).split(":")
Jh, Jm, Js = str(Juhrano).split(":")
Tsec, Jsec = Ts.split("."), Js.split(".")
Ts, Js = Tsec[0], Jsec[0]
Th, Tm, Ts, Jh, Jm, Js = int(Th) + 24*Tday, int(Tm), int(Ts), int(Jh) + 24*Jday, int(Jm), int(Js)
if not "-" in str(datetime.timedelta(hours = Th - Jh, minutes = Tm - Jm, seconds = Ts - Js)):
Tondauhral = datetime.timedelta(hours = Th - Jh, minutes = Tm - Jm, seconds = Ts - Js)
Janekuhral = "0:00:00"
else:
Tondauhral = "0:00:00"
Janekuhral = datetime.timedelta(hours = Jh - Th, minutes = Jm - Tm, seconds = Js - Ts)
print("Tonda", Tondauhral)
print("Janek", Janekuhral)
soubor = open("C:/Users/Tonda_2/Documents/Python/Cas/cas.txt", "w")
soubor.write("Tonda" + " " + str(Tondauhral) + "\n" + "Janek" + " " + str(Janekuhral))
soubor.close()
###
zaznam = open("zaznam.txt", 'a')
oldcas = open("cas.txt", "r").read().replace("\n", ";")
zaznam.write("\t" + oldcas)
zaznam.close()
###
import os
if turnoff == "":
os.system("shutdown -l")
else:
os.system("shutdown -s -f")
#import subprocess
#subprocess.call(["shutdown /l", "-f", "-s", "-t", "5"]) #subprocess.call(["shutdown.exe", "-f", "-s", "-t", "5"])