K
K
Konstantin2021-06-25 12:54:05
Python
Konstantin, 2021-06-25 12:54:05

How to change the values ​​of variables through another python file?

I'm trying to implement saving settings to another python file, but in vain. Google didn't help.

Data.py settings file (may be needed):

level = 1 # Уровень
music = True # Включена ли музыка
sfx = True # Спецэффекты

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
o5a, 2021-06-25
@Fire_Fall

Of the simple options
1. configparser
Automatically reads and writes changed values ​​​​to files of the format
variable = value
2. json
We store our changed variables in a dictionary and write / read to the json file.

import json

config = {
"level": 1,
"music": True,
"sfx": True,
}

# запись
with open("config.json", "w") as f:
    json.dump(config, f)

# чтение
with open("config.json") as f:
    config = json.load(f)

# использование соответственно в виде словаря
config['level'] = 3

R
Ronald McDonald, 2021-06-25
@Zoominger

And why in the Piston file?
Save to some CSV and don't suffer. Plus, Piston has ready-made libraries for working with configs.

V
Vadim Shatalov, 2021-06-25
@netpastor

DB, radish, text file - choose any option

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question