Updating Variables in Files
Clash Royale CLAN TAG#URR8PPP
Updating Variables in Files
I'm trying to change multiple variables in another file, but only one variable will get updated unless I close out and reenter the program. This is a simple version of the code:
import pyVars
def loop():
newVar = int(input('New var: '))
temp1 = newVar
temp2 = pyVars.varA
temp3 = pyVars.varB
f = open('pyVars.py', 'w')
f.close()
f = open('pyVars.py', 'a')
f.write('varA = ' + str(temp1) + 'n')
f.write('varB = ' + str(temp2) + 'n')
f.write('varC = ' + str(temp3) + 'n')
f.close()
f = open('pyVars.py')
print(f.read())
f.close()
loop()
loop()
And I get the variables from another file I created in the same folder:
varA = 0
varB = 0
varC = 0
Only varA keeps changing. How do I fix this?
varB
varC
temp2
temp3
What do I do to them?
– Wyxlor
Aug 12 at 4:23
I have deleted both edits to my answer as they were obviously incorrect. Working on your code at the moment.
– Chaitanya Bangera
Aug 12 at 5:40
@Wyxlor I didn't understand what it was you were trying to do. I see it now and have posted how to do it in the answer below.
– berkelem
Aug 12 at 7:16
2 Answers
2
You can use Python's clever variable swapping syntax as follows:
import pyVars
def loop():
newVar = int(input('New var: '))
pyVars.varA, pyVars.varB, pyVars.varC = newVar, pyVars.varA, pyVars.varB
with open('pyVars.py', 'rb') as f:
print(f.read())
loop()
loop()
Just curious, why do you open the file in binary reading and not regular?
– Wyxlor
Aug 13 at 17:01
Honestly, it's force of habit. But there are reasons for it, especially if you want your code to work across platforms (e.g. Linux, Windows). The docs talk about it here: docs.python.org/2/tutorial/…
– berkelem
Aug 13 at 17:22
Did this answer your original question? If not, please tell me why not.
– berkelem
Aug 14 at 14:47
This most probably has to do with the import statement. When you write:
import pyVars
You get varA=0, varB=0, varC=0 as copies of the pyVars variables.
No matter how many times you change the values of varA, varB, varC, whenever you try to read pyVars.varA, pyVars.varB, it is always returning 0 as that was the vaues it had imported. I initally set varA=700, varB=800, varC=900 and debugged, and on each loop, the initial values of varA was still 700. I checked the initial values by making changes to the code:
from pyVar import varA, varB, varC
While debugging, varA always returned 700, even though the actual value of varA was different in pyVars.py file.
That is also the reason why only varA is getting updated on your pyVars.py file. A is getting the input from the user, but B and C were set to the values of A and B, which were imported as 0. So they always stay as 0.
This is my understanding after fiddling with the code for more than 30 mins now. I would be happy to be corrected by someone, but nevertheless, it has been a great question/learning experience for me. Thank you!
Edit: So I have finally got it to work. It was related to the import statement. Since you are changing the source file that was imported, you will need to reload the file if you need to use the new values. Please see the code that I have changed, which works for me:
import importlib
def loop():
import pyVars
pyVars = importlib.reload(pyVars)
newVar = int(input('New var: '))
temp1 = newVar
temp2 = pyVars.varA
temp3 = pyVars.varB
f = open('pyVars.py', 'w')
f.close()
f = open('pyVars.py', 'a')
f.write('varA = ' + str(temp1) + 'n')
f.write('varB = ' + str(temp2) + 'n')
f.write('varC = ' + str(temp3) + 'n')
f.close()
f = open('pyVars.py')
print(f.read())
f.close()
loop()
loop()
So is there no way to update the other variables?
– Wyxlor
Aug 12 at 15:41
I have updated the answer with the code that works for me now. Basically, I have moved the import inside the loop, and added a reload, so it uses the updated value every time. Hopefully, this solves your issue
– Chaitanya Bangera
Aug 12 at 17:38
You can update all the variables using variable swapping syntax. See my answer.
– berkelem
Aug 13 at 2:02
I found another way - using the reload function in importlib. It refreshes the library and updates the variables.
– Wyxlor
Aug 13 at 17:50
That is exactly what I had suggested in the code above yesterday :)
– Chaitanya Bangera
Aug 13 at 18:02
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
For
varB
andvarC
to change, you would have to do something totemp2
andtemp3
. As it is you're just reading and writing the same value over and over again.– berkelem
Aug 12 at 4:14