简体   繁体   English

Python:如何让脚本在运行时写入文件,然后保持打开状态以在按键上进行写入?

[英]Python: How can I have a script write to a file when it's run then stay open to write on a keypress?

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. 我正在尝试编写一个脚本,该脚本在运行时会将当前时间写入文件,然后希望它在后台保持打开状态,以便每当我按插入键时,它将写入当前时间到文件减去7分钟。

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). 就像这样,如果我标记出使它保持打开状态以寻找按键的块(我正在使用pyhook),它将仅记录我运行它的时间。 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. 因此,我可以肯定的是,一旦它开始寻找密钥后便会清除文件,这是一个问题,但是我不确定原因,尤其是因为在密钥输入之前,它甚至不应该运行任何与文本文件相关的代码被按下,关闭了我的if语句。

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? 是否可以在同一脚本中同时使用这两个脚本,还是我必须将它们分开并用.bat之类的东西调用它们?

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.) (同样,如果您想知道为什么在世界上我想要一个可以做到这一点的程序,我应该与兄弟姐妹共享台式机,并且我们会记下我们已经工作了多长时间了我们开始的时间。这样,我应该能够使其自动化,并且有一种方法可以在被要求下车时谨慎地设定时间。)

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()

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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试在批处理文件中使用python脚本写入文件然后运行它时。 它不会写入文件 - When I try to write to a file using python script in a batch file and then run it. It doesn't write to the file 当我运行它的 .py 文件时,无法在 python 中写入文件。 但是当我在pycharm中运行它时,它确实有效 - Can't write files in python when I run it's .py file. But when I run it in pycharm, it actually work 当我打开要首先写入的文件时,为什么在File.open中无法读取任何内容? - Why I can't read anything with File.open in python when I open the file to write first? 使用Python的“ with open()”编写日志,如何向日志写入异常? - Using Python's 'with open()' To Write a Log, How Can I Write Exceptions to my Log? 如何在 Python 中直接在 s3 中的文件中写入 JSON? - How can I write JSON in file in s3 directly in Python? 如何编写一个在我写 = 时停止的脚本? - How can a write a script that stop when I write =? 作为systemd服务运行时,Python脚本不会写入文件 - Python script doesn't write to file when run as a systemd service 如何编写Python脚本以正确打开csv文件 - How to write a Python script to open a csv file correctly 为什么我无法写入在python中打开的文件? - Why can't I write to a file that I open in python? 如何在 python 脚本中运行 postgres 命令并将其写入文件? - How to run a postgres command in python script and write it into a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM