1

I have a Python file file1.py with the following code lines:

uname="[email protected]"
pwd="abcdef"

I want to replace and change the uname to "auto2mailnesia.com", then to "[email protected]" and so on and then use the updated uname in another file. Consider a scenario where I want to update uname from auto1 to auto3.

I have another Python file file2.py with the below code lines:

from xxx.xxx import file1

for i in range(0,1):
    currentuser = (uname[uname.index('auto') + 4:uname.index('@')])
    newuser = str(int(currentuser) + 1)
    newusername = uname.replace(currentuser, newuser)
    print(uname)
    print(currentuser)
    print(newusername)
    print(newuser)

    with open(testdataFileName, 'r+') as f:
        text = f.read()
        text = re.sub(uname, newusername, text)
        f.seek(0)
        f.write(text)

When I run file2.py in a loop of 1, then the uname gets correctly updated in file1.py

Output is as follows:

[email protected]
1
[email protected]
2

But, when I run file2.py in a loop of say 3 then the uname gets updated in file1.py only once.

Output is as follows:

[email protected]
1
[email protected]
2
[email protected]
1
[email protected]
2
[email protected]
1
[email protected]
2

I do not understand why uname is being updated only once in file1.py while running in a loop.

Can someone please give an explanation for this?

Also, if someone could tell me what I am doing wrong and how to fix this it would be very much appreciated.

15
  • Does this answer your question? Writing to a file in a for loop Commented Aug 2, 2020 at 4:56
  • What are the values that you actually print? Now, look at the code. Why should those values get updated by the file contents? You changed the contents of the file, but why should that matter? Commented Aug 2, 2020 at 5:02
  • Anyway, if you want to verify that the file contents changed, the most accurate way to do that is to actually examine the file, for example by opening it in a text editor. Commented Aug 2, 2020 at 5:03
  • If i understand correctly, you want to run file2.py and it reads file1.py. Then convert the value in file1.py where uname="[email protected]" to uname="[email protected]". If you run it multiple times, I assume you want it to create new sets of uname & pwd pairs where the username keeps incrementing from 1 to 2 to 3 to 4 to .... as many times you run. Is my understanding correct? Commented Aug 2, 2020 at 5:04
  • 1
    Yes; you're storing it in text. What happens the next time through the for loop? The first thing that happens is currentuser = (uname[uname.index('auto') + 4:uname.index('@')]). But uname has the same value now that it did before, therefore currentuser will get the same value that it did last time, and so on. When you open the file this time around you will read the text again, but then attempt to replace the same uname with the same newusername. This is why the uname only gets updated once, as you complained: because nothing in the loop changes the value of uname. Commented Aug 2, 2020 at 12:28

2 Answers 2

1

Your problem is that uname remains the same through every iteration (no matter what i is). It's not updated at the end of each iteration of the loop. Instead, I recommend keeping variables currentusername and currentuser so that the last username and index (or ID) can be preserved and therefore accessed. These will be initialized before the loop begins, and updated before the start of each next iteration.

import re
uname="[email protected]"
pwd="abcdef"

# initialize
currentusername = uname
currentuser = (uname[uname.index('auto') + 4:uname.index('@')])

for i in range(0,4):
    print('\niteration ' + str(i))
    newuser = str(int(currentuser) + 1)
    newusername = currentusername.replace(currentuser, newuser)
    print("uname=" + uname)
    print("currentuser=" + currentuser)
    print("newusername=" + newusername)
    print("newuser=" + newuser)

    with open('somefile.dat', 'r+') as f:
        text = f.read()
        text = re.sub(currentusername, newusername, text)
        f.seek(0)
        f.write(text)

    # update for next iteration
    currentuser = newuser
    currentusername = newusername

Output:

iteration 0
[email protected]
currentuser=1
[email protected]
newuser=2

iteration 1
[email protected]
currentuser=2
[email protected]
newuser=3

iteration 2
[email protected]
currentuser=3
[email protected]
newuser=4

iteration 3
[email protected]
currentuser=4
[email protected]
newuser=5

At the end, somefile.dat would include [email protected].

Sign up to request clarification or add additional context in comments.

3 Comments

I am changing the value of uname and storing the new changed value in newusername Then i am replacing uname in file1 by newusername by the below lines ` with open(testdataFileName, 'r+') as f: text = f.read() text = re.sub(uname, newusername, text) f.seek(0) f.write(text) `
I ran your code and getting the following output iteration 0 [email protected] currentuser=1 [email protected] newuser=2 iteration 1 [email protected] currentuser=2 [email protected] newuser=3 iteration 2 [email protected] currentuser=3 [email protected] newuser=4 As you can see above, the uname is the same in each iteration, then how come currentuser changes each time, because as far as i understand, currentuser is calculated from uname
It's calculated from uname originally but then updated in the loop near the bottom. Hope that makes sense.
1

I know you have a lot of answers already. I thought I will simplify your code a bit. You need to read only the first line.

with open('your_filename.txt', 'r+') as f: #update your_filename with your filename
    text = f.readline()
    print ('previous value in file :', text)
    cuser = int(text[11:text.index('@')]) + 1
    text = text[:11]+str(cuser)+text[text.index('@'):]
    f.seek(0)
    f.write(text)
    print ('new value in file : ', text)

The output each time you run changes. My first run gave me:

previous value in file : uname="[email protected]"

new value in file :  uname="[email protected]"

The contents in the file shows as:

uname="[email protected]"
pwd="abcdef"

My 4th run gave me:

previous value in file : uname="[email protected]"

new value in file :  uname="[email protected]"

The contents in the file shows as:

uname="[email protected]"
pwd="abcdef"

As you can see, the file keeps getting updated every time i run the code. I am not sure why you need to loop it a few times. Are you trying to change the value in the file a few times for each run?

I am not sure if you really need a loop. I updated my code to create a loop and it still worked. Here's the updated code. I just used a simple for loop

for i in range(4):

    with open('xyz.txt', 'r+') as f: #update your_filename with your filename

        text = f.readline()

        print ('previous value in file :', text)

        cuser = int(text[11:text.index('@')]) + 1
        text = text[:11]+str(cuser)+text[text.index('@'):]

        f.seek(0)
        f.write(text)

        print ('new value in file : ', text)

Here's the output I got:

previous value in file : uname="[email protected]"

new value in file :  uname="[email protected]"

previous value in file : uname="[email protected]"

new value in file :  uname="[email protected]"

previous value in file : uname="[email protected]"

new value in file :  uname="[email protected]"

previous value in file : uname="[email protected]"

new value in file :  uname="[email protected]"

The value in the file is:

uname="[email protected]"
pwd="abcdef"

3 Comments

Thanks for the detailed explanation Your code works great I am running this in a loop since I need to test logins for say suppose 10 users, from auto1 to auto 10 (in future this can go on to 100 or 1000 users) and i do not want to have 10 different uname stored in file1.py i am reading uname from file1.py and then passing it as a parameter to login function Your code will read and change the first line from file1.py Suppose i have uname on line1, pswd on line2, phone number on line3, employee id on line 4 and i only want to change line1,3,4, not the pswd. How to do that
In that case, you will have to read the file in a loop. and change the information on each line. for f1 in f: will help you read each line. You can check the contents of each line and make the changes.
If it solved your question, you can flag my response as answered. Also, if you want to update your question with the new source file, I can edit the response with the code to solve for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.