0

I am currently trying to write to another python file to replace a variable using user input.

Suppose I have file_a.py and file_b.py

I want to execute file b to grab a variable from file a. Then save that userinput value to var1 in file_a.py.

Example:

file_a.py contents:

var1 = "/path/to/file"

file_b.py contents:

print(var1 " is the current path") 

userinput = input("Type new directory path here: ")
Ender Look
  • 2,303
  • 2
  • 17
  • 41
somerandomguy95
  • 161
  • 1
  • 3
  • 13

2 Answers2

2

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.

Ender Look
  • 2,303
  • 2
  • 17
  • 41
  • Regarding, your sentence about not being able to modify the other file, how would I possibly do this? I know I can write to files but I don't want to just do a normal append or write, I want to overwrite the value of the variable itself. – somerandomguy95 Dec 07 '17 at 04:00
  • @somerandomguy95 So you **don't** want to use `open()` and normal writing output file functions? – Ender Look Dec 07 '17 at 04:04
  • Sorry if you misunderstood me, I meant that I do want to use open and write, I just don't know how to use them. For example, I don't want to ONLY append to the end of a file or rewrite the whole file. I just want to overwrite the value for var1 which is not something I am not sure about. I am thinking maybe I can use some module to search for the certain line where the variable is and just rewrite the whole line? – somerandomguy95 Dec 07 '17 at 04:15
  • Thank you I will try it out as soon as I can. But essentially from what I see you're opening filea and reading it to look for var1, then basically defining the new text and split it from var1 so we can input the new data and then opening the file to actually write with the new data later, is that basically correct? I just want to make sure I understand because then whats the point if I just copy it. – somerandomguy95 Dec 08 '17 at 00:47
  • Yes. I open the data, then I started reading the file line by line -appending them to a list- and searching for `var1`, when I find it, I split **that** line into 2 items: before `var1` (to get the same identation) and after var1 (we don't need that), and I append to the list **the identation `line.split("var1")[0]`, the name of the variable `var1`, equal `=` and the new value `new`** (remember the `''` because it is an string). Finally I make a new file and write all the items (lines) from the list. But I have to be sincery with you... I don't think the teacher is looking for this exactly... – Ender Look Dec 08 '17 at 03:17
  • Thank you for explaining it and all of your help, it's okay. As long as it works I'm happy. – somerandomguy95 Dec 08 '17 at 04:40
1

Are you trying to edit the text of file_a.py? If so then you could look into the package redbarron. If you are importing file_a as a module, you can edit its global variables as such:

import file_a file_a.var1 = 'new value'

This is called "monkey patching" and the effect will be inplace until the program exits. Executing it next time will have no effect.

However, I feel like this question is likely misguided. Can I ask what you are trying to accomplish?

Erotemic
  • 4,806
  • 4
  • 39
  • 80
  • Essentially its for a school assignment. Prof wants me to prompt a user for a new file path that you can change through another file. Meaning the user is not able to change the file itself, the only portion the user would see is the terminal prompt. I understand how to read from another file, but I actually want to write the new value to that variable in file_a – somerandomguy95 Dec 07 '17 at 03:59
  • file_a has to be a python file, and it does not have only just that line. It has other data but it is not relevant so I did not attach it. But thank you, I will look at the link. – somerandomguy95 Dec 07 '17 at 04:05
  • @somerandomguy95, `file_a` is not necessary a `.py`? It **only** have **that** line? I am thinking right now in some interesting ideas... if the first one is "yes" look at [configparser](https://docs.python.org/3/library/configparser.html), if the first *or* second question is "yes" look at [Reading and Writing Files](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)... – Ender Look Dec 07 '17 at 04:10