If both files are in the same folder you can:
file_b.py
from file_a import var1
print(var1 " is the current path")
userinput = input("Type new directory path here: ")
var1 = userinput
Or
import file_a
print(file_a.var1 " is the current path")
userinput = input("Type new directory path here: ")
file_a.var1 = userinput
But, Do you know that you can't modify the another file itself? var1
will be changed in the memory, but if you open file_a.py
, var1
will be "/path/to/file".
P.S: A little suggestion print(var1 " is the current path")
--> print(var1, "is the current path")
.
Edit: I think this will be good by your level (you are doing this like homework, if I make something more efficient or with some modules the teacher would notice it).
def re_write(new):
with open("file_a.py", 'r') as file:
new_file = []
for line in file:
if "var1" in line:
new_file.append(line.split("var1")[0] + "var1 = '" + new + "'")
else:
new_file.append(line)
with open("file_a.py", 'w') as file:
for line in new_file:
file.writelines(line)
A better code (faster, with less memory usage and maybe better in general) could be find in this question.