-1

So I have a unique player ID string that is stored in a JSON file after it detects on start-up that the player is new to the game.

def checkIfPlayerIsNew():

    if os.path.exists("myJson.json"):
        print('file exists')

        k = open('myPlayerIDs.json')
        print(k.read())
        #print("This is the k value: {}".format(code))
        #code = json.dumps(k)

        getData(k.read())

    else:
        print('file does not exist')

        f = open('myJson.json', 'x')  #creates the file if it doesn't exist already
        f.close()

        file = open('myPlayerIDs.json', 'w')
        file.write(json.dumps(str(uuid.uuid4())))
        file.close

        checkIfPlayerIsNew()

Now, if it detects that the player is not new it gets the ID from the JSON File and passes it to the get data function below

def getData(idCode = "X"):

    print('the id code is this: {}'.format(idCode))
    outputFile = json.load(open('myJson.json')) 
    for majorkey, subdict in outputFile.items():
        if majorkey == idCode: 
            for subkey, value in subdict.items():
                print('{} = {}'.format(subkey, value))
                #playerData[subkey] = value
        else:
            print("ID Does not match") 
            break

The problem is that when I check the id code in the get data function it prints out a blank space as if the id code has been changed to nothing (which I can't figure out why it has done that) and prints this out to the terminal:

enter image description here

The playerID JSON File:

enter image description here

4
  • How would multiple player IDs be represented in your JSON file? Also, please fix your code's indentation.
    – jarmod
    May 20, 2022 at 13:26
  • @jarmod good point, was just making this as an example for myself to learn python file reading with JSON. also the indentation looks fine to me except the def part of the function being in line with the if statement. which is just me spacing it for my question.
    – Astrum
    May 20, 2022 at 13:42
  • The posted code is not indented correctly. It's important in Python and on StackOverflow.
    – jarmod
    May 20, 2022 at 13:45
  • @jarmod edited for you
    – Astrum
    May 20, 2022 at 13:50

1 Answer 1

2

You can't read() a file twice without seeking back to the start. The right thing to do here is to read the file into a variable:

if os.path.exists("myJson.json"):
    print('file exists')

    # Read file into a variable and use it twice
    with open('myPlayerIDs.json', 'r') as k:
        data = k.read()

    print(data)
    getData(data)
#...
5
  • 2
    k = open() is bad practice. Either use with as a context manager or make sure you k.close() after you read it May 20, 2022 at 13:30
  • Good point, edited.
    – Wacov
    May 20, 2022 at 13:36
  • I keep thinking once I've opened the file its open until I close it, thanks for your answer
    – Astrum
    May 20, 2022 at 13:37
  • @LukasSchmid if I use it the 'with' way why is that better?
    – Astrum
    May 20, 2022 at 13:43
  • with auto-closes the file for you when the with block ends, so you don't need to remember to call close(). As for why you should close the file: stackoverflow.com/questions/25070854/…
    – Wacov
    May 20, 2022 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.