Python Forum

Full Version: Appending to a text string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi All

I have the following code which reads the contents of a file, appends 1,2,3, etc to the contents and then writes the results to a new file:

f = open("text-file.txt", "rt")
s = f.read()
f.close()
f = open("new-text-file.txt", "wt")
f.write(s + '1')
f.write(s + '2')
f.write(s + '3')
f.close()
The code is working but I want to append to the text string 200 times and wondered if there is a while loop or something like that I can use to achieve the same thing. I don't want to write out f.write(s + '4') 200 times.

Thanks for your help.
Use a for loop with range
for count in range(1,201):
    print(count)
Output:
1 2 3 ... ... ... 198 199 200
Set a count that keeps track of the number of times and then use while loop like :
count = 0
while count <= 200 :
    # your code
    count += 1
Hi there

Thanks for the reply. Now I've got:
f = open("password.txt", "rt")
s = f.read()
f.close()
f = open("password2.txt", "wt")

count = 0
while count <= 200 :
    f.write(s)
    count += 1  

f.close()
However, the code prints the contents of the file 200 times. How do I append the count to the string? i.e, the contents of the file says password. I want the output to read password1password2password3, etc..

Thanks
Adjusted above code
text = 'password'

for count in range(1,201):
    print(f'{text}{count}')
Output:
password1 password2 password3 ... ... ... password198 password199 password200
If you already have a file with password,then you do not need a new loop to add to those password.
Just iterate over password and add enumerate() for numbering.
Output:
abc def ghi
with open('password.txt') as f,open('new_password.txt', 'w') as f_out:
    for index,line in enumerate(f, 1):
        line = line.strip()
        f_out.write(f'{line} {index}\n') 
Output:
abc 1 def 2 ghi 3
If just want to create this from a constant string,then just write to file what @Yoriz posted.
Hi All

I think I'm nearly there with the code:

f = open("file.txt", "rt")
s = f.read()
f = open("new-file.txt", "wt")

for count in range(1,201):
    s+=str(count)
    print({s},{count})
   
f.write(s) 
f.close()
However, its printing the string within the file followed by the count, for example its writing to the new-file like this:

password12345678910111213, etc

What I'm looking to achieve is the below:

password1password2password3, etc.

Thank you
Did you look at my post?
What dos your file.txt contain,are the new password on each line that you want to add numbering to?

When you do this it read all(whole file) to one text object,and you can not add numbering as all will be added last as you show.
s = f.read()
See how i loop over file content and add to numbering to each line.
Hi

I tried your code and I got the following error message. I'm using Python 2.7:

invalid syntax: test.py, line 4, pos 39 in file C:\python-scripts\test.py
f_out.write(f'{line} {index}\n')

The print operation is doing this correctly:

f = open("password.txt", "rt")
s = f.read()
f = open("password2.txt", "wt")

for count in range(1900,2021):
    s+str(count)
    print({s},{count})
But how do I write this operation to a file. I only have one line in the file which is password.

Thanks
Well that must be the problem - you are using python 2.7 - therefore that must be a reason the code isn't working? Switch to python 3
Pages: 1 2