'How to create and open a json file?

Does anybody know why this code doesnt work? We are trying to create a json file and write some information in it. The aim is to create a user account, save the password and name in a json file and then login and go on with other functions. See the code below. Any help appreciated!!

player_data_file = 'player_portfolio.json'
with open(player_data_file, 'r') as f:
    if os.stat(player_data_file).st_size == 0:
        data = {}
    else:
        data = json.load(f)
    data.update({player_name: {"balance": balance, "portfolio": {}}})
with open(player_data_file, 'w') as f:
    json.dump(data, f, indent=4)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source