#๐ permanently changing variables
45 messages ยท Page 1 of 1 (latest)
@runic eagle
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.
You can't. You need to save that data to the disk before closing, and then re-read it and restore the variable yourself when the program is re-opened.
soo I need to make a separate file and read and save it in there?
Yes. A simple text file holding JSON, or a full database are typical options; depending on how much data you have.
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)
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
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')```
tysm ill save that
Honestly, I'd just use JSON instead of manual parsing
Thats just to open the file, you'll need the one to write to it as well
JSON is probably better tbf, and pyhton works well with it
!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)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | {"var1": 1, "var2": 2}
002 | {'var1': 1, 'var2': 2}
You'd need to then put this in a file right
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.
This typically looks like:
with open(path) as f:
loaded = json.load(f)
Then loaded['var1']
small question
I can write something in a moment
why not just json.load(open(path))
does it change anything
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.
Python closes files when the object they're associated with is destroyed, but if the object isn't destroyed, you can run into problems.
None of this is Python-specific. You
'd likely have siumilar questions in most other languages.
Well, I shouldn't say "none".
well im braindead i get that
Other languages would just let you leak file handles instead of closing them for you.
i will pretend i understand you
A file handle is the thing the OS uses to track files that are in use
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.