Python Forum
Change variable in an outside file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change variable in an outside file
#1
a.py
ssid = 'myssid'
password = 'mypass'
ap_mode = 'yes'
b.py
import a

print(a.ap_mode)
# output -> yes

# modify variable
a.ap_mode.append('no') # not correct
print(a.ap_mode)
# output -> yes "it should be no"
Hi,

How do I change a variable from one file to another without using json/dict/list?

TIA
Reply
#2
Your problem has nothing to do with objects being in different modules and everything to do with str not knowing how to "append()". It would work if you used a.ap_mode='no' instead a.ap_mode.append('no')
junk2.py
x = 5
def print_x():
    print(x)
junk.py
import junk2
junk2.x = 1
junk2.print_x()
Output:
1
Reply
#3
(Nov-11-2020, 03:57 AM)deanhystad Wrote: Your problem has nothing to do with objects being in different modules and everything to do with str not knowing how to "append()". It would work if you used a.ap_mode='no' instead a.ap_mode.append('no')

That was my first choice but it doesn't work.

With your example, your're changing the variable when in use but x still remains 5 it doesn't change to 1.
Reply
#4
I also tried this with no avail:
#read input file
fin = open("a.py", "rt")
#read file contents to string
data = fin.read()
#replace all occurrences of the required string
data = data.replace('ap_mode =' + "'no'", 'ap_mode =' + "'yes'")
#close the input file
fin.close()
#open the input file in write mode
fin = open("a.py", "wt")
#overrite the input file with the resulting data
fin.write(data)
#close the file
fin.close()
Reply
#5
This problem has nothing at all to do with variables in other files and everything to do with how import works. My previous example changed the value of a variable in another module, but that was not the question you are asking. You want to know how to make a change persistant.

To make your program permanently remember anything it has to be saved in a file. The file is is usually a database file or a json file or just a text file, but I suppose you could modify a .py file and use that to remember a variable's value. One problem with a .py file is that to see any changes you need to reload the module. Also any objects created in the old module will not change to reflect changes made to the .py file. This makes using a .py file kind of a one shot thing. You can only change the file and expect the changes to take affect the next time you start your program and import the modified file.
ebolisa and buran like this post
Reply
#6
(Nov-11-2020, 04:35 AM)deanhystad Wrote: You can only change the file and expect the changes to take affect the next time you start your program and import the modified file.

Ok, got it. For the variable to change in file 'a' I should better use dictionary/json format. Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  logging: change log file permission with RotatingFileHandler erg 0 957 Aug-09-2023, 01:24 PM
Last Post: erg
  How can I change the uuid name of a file to his original file? MaddoxMB 2 868 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 918 Feb-15-2023, 05:34 PM
Last Post: zsousa
  find some word in text list file and a bit change to them RolanRoll 3 1,482 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
Photo (Beginners problem) Does file change during this code? fiqu 3 1,849 Nov-03-2021, 10:23 PM
Last Post: bowlofred
  change csv file into adjency list ainisyarifaah 0 1,479 Sep-21-2021, 02:49 AM
Last Post: ainisyarifaah
  Use Python to change a PHP file on my little webpage Pedroski55 0 1,481 Aug-28-2021, 12:42 AM
Last Post: Pedroski55
  how can a variable change if I haven't changed it? niminim 5 2,992 Apr-07-2021, 06:57 PM
Last Post: niminim
  how to change the range of read CSV file every time python file runs greenpine 6 4,379 Dec-08-2020, 10:11 PM
Last Post: greenpine
  Change variable value during a while loop? penahuse 2 3,988 Nov-15-2020, 11:53 PM
Last Post: penahuse

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020