简体   繁体   中英

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. I used pyinstaller in an attempt to create a straight forward standalone version of the script. 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? 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. 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. Try resolving this too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM