Hello, can you help me with some guidance?

Good afternoon people, I need your help,

I am trying to write a script that will open my file with the network data and modify it.
My progress so far:

1- I opened the file and it reads each line and asks if you want to modify it:

Variable1 = open(’/path/… la interface que el usuario escriba)

for Variable2 in Variable1:
#Ask to change any value in the configuration file)
Variable3 = input('Modify: ’ + Variable2 + ’ Y/N: ').capitalize()
if Variable3 == ‘Y’:
input(‘Type the new value’)
else:
print(‘Value not changed’)

I don’t know how to continue. I need what the user types, to modify the value of that line in that file. Any ideas?

Thank you

You need to save what the user types in a variable:

if Variable3 == 'Y':
    what_the_user_types = input('Type the new value')

Hello, @seba.rvr, and welcome to the Python Forum!

The names of variables used in this line are acceptable to the Python interpreter:

Variable3 = input('Modify: ' + Variable2 + ' Y/N: ').capitalize()

However, in Python, it is conventional to begin the names of variables with a lowercase letter, except in certain contexts.

This long document, which you do not need to read in its entirely, discusses recommended style for Python programs:

Inside that document, we can find this:

Function and Variable Names

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Variable names follow the same convention as function names.

Following that convention, you might have:

variable3 = input('Modify: ' + variable2 + 'Y/N: ').capitalize()

EDIT (2x on November 20, 2021):

You could also consider doing it this way to improve readability:

variable_3 = input('Modify: ' + variable_2 + 'Y/N: ').capitalize()

Also consider using meaningful variable names. For example, the name variable_3 might not make obvious the purpose of that variable.

Hello Quercus,

Thank you for your response.

Yes, I understand about the variable names. I just use them like that in the message as an example.

I asked user what I need and I put the answers into a variables.
Then I open the file I need to read and look for info to replace or add.

Here is my problem.
How can y find in on line, for example: ONBOOT=‘yes’ and replace that with the user answer no.
Also, How I add a new line with other user answer that is not already in that file (for example IP=192.168.114.1 or whatever).

Finally I need that that file keep that changes on it.

Thank you for your help!.
Sebastian!

Thank you Steven. I worked on that. But I’m having problems with find, replace and save text in one file. Look my next reply, maybe you can help me to do that.

Thank you again!

Pattern matching, with replacement of portions of the matched patterns, would be useful for this. A good module to consider would be regular expressions. See:

While there is a lot to read there, some of it could be really helpful.

Thank You, Y can solve it with your help.