Fuze Test
Fuze Test

Reputation: 31

Python does not write to a file when running in a loop

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.

Upvotes: 1

Views: 184

Answers (2)

Joe Ferndz
Joe Ferndz

Reputation: 8508

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"

Upvotes: 1

ELinda
ELinda

Reputation: 2821

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].

Upvotes: 1

Related Questions