0

I'm trying to write a script that when I run it, it will write the current time to a file, then I want it to stay open running in the background so that any time I press the Insert key it will write the current time to the file minus 7 minutes.

I have both these functions working as I want, however they only work individually. As in, it will only write the time it is when I run it if I mark out the block that keeps it open searching for a keypress (I'm using pyhook for that). And it will only stay open looking for a keypress if I mark out the part that would write the time it was when I first ran it. Here's my code

import pyHook, pythoncom
from datetime import datetime

#writes the time when it starts bit
t = datetime.now()
h = t.hour
m = t.minute
s = t.second

if h > 12:
    h = h - 12

if m < 10:
    m = str(m).zfill(2)

file = open('time.txt', 'w')
file.write('Jeremiah got on at ' + str(h) + ':' + str(m))


#stays open and looks for a keypress bit
hookManager = pyHook.HookManager()

def OnKeyboardEvent(event):
    keyPressed = event.KeyID
    if keyPressed == 45:
        t = datetime.now()
        h = t.hour
        m = t.minute
        s = t.second

        if h > 12:
            h = h - 12

        if m < 10:
            m = int(str(m).zfill(2))

        if m - 7 < 0:
            h = h - 1
            sub = m - 7
            m = 60 + sub
        else:
            m = m - 7

        file = open('time.txt', 'w')
        file.write('Jeremiah got on at ' + str(h) + ':' + str(m))
    return True


hookManager.KeyDown = OnKeyboardEvent
hookManager.HookKeyboard()
pythoncom.PumpMessages()

If I mark out neither sections, it will clear the text file when it starts and then stay open to look for the keypress. Marking out just the startup time section will keep it open looking for the key properly, and it won't clear the text file. So I'm pretty certain it's some problem that once it starts looking for a key it clears the file, but I'm not exactly sure why, especially since it shouldn't even run any code to do with the text file until the key is pressed, setting off my if statement.

Is it possible to have these two in the same script, or do I have to separate them and call them both with a .bat or something?

Thanks in advance.

(also if you're wondering why in the world I would want a program that would do this, I'm supposed to share the desktop computer with my siblings, and we keep track of how long we've been on by writing down the time we got on. This way I should be able to automate it, as well as have a way to discreetly set my time forward when asked to get off.)

2 Answers2

0

I got it. I just had to close the file after writing to it in the startup section.

file = open('time.txt', 'w')
file.write('Jeremiah got on at ' + str(h) + ':' + str(m))
file.close()
0

To make things easier you can do this so you don't have to bother about closing the file because it will just close on it's own.

With open ('time.txt', 'w') as file:
    file.write('Jeremiah got on at ' + str(h) + ':' + str(m))
gerj
  • 1
  • 1