[英]Python Script not updating file contents while using a pyinstaller version of the script on a machine without python installed
So my issue right now is that I wrote a script that would update contents within a file each time you hit an arrow key.所以我现在的问题是我写了一个脚本,每次你按下箭头键时都会更新文件中的内容。 The script works great on my PC.
该脚本在我的 PC 上运行良好。 I used pyinstaller in an attempt to create a straight forward standalone version of the script.
我使用 pyinstaller 试图创建脚本的直接独立版本。 The problem right now is that the script is not seemingly updating their files, Within the console of the application, it does show that it is properly counting up the scores.
现在的问题是脚本似乎没有更新他们的文件,在应用程序的控制台中,它确实表明它正在正确计算分数。 but upon opening the file it stays at 0. Files are not in read-only?: any ideas?
但是在打开文件时它保持在0。文件不是只读的?:有什么想法吗? Code below:
下面的代码:
import os
import time
import threading
from pynput import keyboard
def on_press(key):
if (key == keyboard.Key.down):
wR = open("Round Number.txt", "w")
w1 = open("Team 1 Score.txt", "w")
w2 = open("Team 2 Score.txt", "w")
wR.write("1")
w1.write("0")
w2.write("0")
print("All values have been reset")
if (key == keyboard.Key.up):
r = open("Round Number.txt", "r")
previousScore = r.read()
w = open("Round Number.txt", "w")
newScore = str(int(previousScore) + 1)
print("Round file has changed to " + newScore)
w.write(newScore)
#print(open("Round Number", "r").read())
if (key == keyboard.Key.left):
r = open("Team 1 Score.txt", "r")
previousScore = r.read()
w = open("Team 1 Score.txt", "w")
newScore = str(int(previousScore) + 10)
w.write(newScore)
print("Team 1 now has " + newScore)
#print(open("Team 1 Score.txt", "r").read())
if (key == keyboard.Key.right):
r = open("Team 2 Score.txt", "r")
previousScore = r.read()
w = open("Team 2 Score.txt", "w")
newScore = str(int(previousScore) + 10)
w.write(newScore)
print("Team 2 now has " + newScore)
# print(open("Team 2 Score.txt", "r").read())
def on_release(key):
pass
#print('{0} released'.format(
#key))
'''if key == keyboard.Key.esc:
# Stop listener
return False'''
#Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
# ...or, in a non-blocking fashion:
listener = keyboard.Listener(
on_press=on_press,
on_release=on_release)
listener.start()```
One of the main mistakes I immediately saw in your code was that you weren't closing the files once you were opening them.我在您的代码中立即看到的主要错误之一是您在打开文件后没有关闭它们。 I hadn't tested your code directly but I did modify it to use
with
as below.我没有直接测试你的代码,但我确实修改了它以
with
如下。 I tested it and it is working fine.我测试了它,它工作正常。 I also created an executable and that too is working fine with values being stored in the files.
我还创建了一个可执行文件,它也可以很好地处理存储在文件中的值。
from pynput import keyboard
def on_press(key):
if (key == keyboard.Key.down):
with open("Round Number.txt", "w") as wR:
wR.write("1")
with open("Team 1 Score.txt", "w") as w1:
w1.write("0")
with open("Team 2 Score.txt", "w") as w2:
w2.write("0")
print("All values have been reset")
if (key == keyboard.Key.up):
with open("Round Number.txt", "r") as r:
previousScore = r.read()
with open("Round Number.txt", "w") as w:
newScore = str(int(previousScore) + 1)
w.write(newScore)
print("Round file has changed to " + newScore)
if (key == keyboard.Key.left):
with open("Team 1 Score.txt", "r") as r:
previousScore = r.read()
with open("Team 1 Score.txt", "w") as w:
newScore = str(int(previousScore) + 10)
w.write(newScore)
print("Team 1 now has " + newScore)
if (key == keyboard.Key.right):
with open("Team 2 Score.txt", "r") as r:
previousScore = r.read()
with open("Team 2 Score.txt", "w") as w:
newScore = str(int(previousScore) + 10)
w.write(newScore)
print("Team 2 now has " + newScore)
def on_release(key):
pass
#Collect events until released
with keyboard.Listener(on_press=on_press,on_release=on_release) as listener:
listener.join()
# ...or, in a non-blocking fashion:
listener = keyboard.Listener(on_press=on_press,on_release=on_release)
listener.start()
One main problem though is that I had to manually create the 3 files and not pressing Down at the beginning caused the program to crash.一个主要问题是我必须手动创建 3 个文件,并且在开始时没有按下Down导致程序崩溃。 Try resolving this too.
也尝试解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.