#๐Ÿ”’ permanently changing variables

45 messages ยท Page 1 of 1 (latest)

runic eagle
#

Hey, if I have a variable x, how do I change it so it stays changed once i close the program?

nova havenBOT
#

@runic eagle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

surreal sun
runic eagle
#

soo I need to make a separate file and read and save it in there?

surreal sun
#

If it's only a couple of variable, JSON would work fine, although Python comes with an SQLite library as well that's easy to use.

#

(Although you need to know SQL for that option)

runic eagle
#

i do know a bit of sql but honestly im js working with 4 integers that i need to save that way so its not necessary

craggy lily
#

I can't vouch for the efficency or quality, but this is what I've been using to store IP and Port between sessions with a script

try:
    config = open('config.txt', 'r')
    for line in config:
        if line.startswith('IP'):
            host = line.replace('IP=','').strip('\n')
        
        elif line.startswith('PORT'):
            port = int(line.replace('PORT=','').strip('\n'))
    config.close()
    print(f'Existing config found\nIP/HOST: {host}\nPORT: {port}\n Would you like to use it?')    
    userResponse = inputValidator('Y/N: ',['y','yes','n','no'])
except:
    print('No config found')```
surreal sun
#

Honestly, I'd just use JSON instead of manual parsing

craggy lily
runic eagle
#

can python read jsons easily?

#

or work with them

craggy lily
#

JSON is probably better tbf, and pyhton works well with it

surreal sun
#

!e

import json

var1 = 1
var2 = 2

stringified = json.dumps({'var1': var1, 'var2': var2})  # Save this to file, or use 'dump'
loaded = json.loads(stringified)  # After loading from file, call this

print(stringified)
print(loaded)
nova havenBOT
craggy lily
surreal sun
#

Yes. Replace dumps and loads with load and dump (the s means "to string"), and then give each call the file to read from/write to.

runic eagle
#

ohh makes sense

#

still kinda confused

#

hm

#

imma try my best

surreal sun
#

This typically looks like:

with open(path) as f:
    loaded = json.load(f)
#

Then loaded['var1']

runic eagle
#

small question

craggy lily
#

I can write something in a moment

runic eagle
#

does it change anything

surreal sun
# runic eagle why not just json.load(open(path))

Because then the file isn't guarenteed to be closed. In theory it will, but if it isn't closed, it will leak references and can prevent your program from opening more files. You should typically always use with when dealing with files to ensure that they're closed.

runic eagle
#

ohh okay tyy

#

python is confusing

surreal sun
#

Python closes files when the object they're associated with is destroyed, but if the object isn't destroyed, you can run into problems.

surreal sun
#

'd likely have siumilar questions in most other languages.

#

Well, I shouldn't say "none".

runic eagle
#

well im braindead i get that

surreal sun
#

Other languages would just let you leak file handles instead of closing them for you.

runic eagle
#

i will pretend i understand you

surreal sun
#

A file handle is the thing the OS uses to track files that are in use

runic eagle
#

ohh

#

ohhh

#

okay imma try to cook now

nova havenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.