1
for don in open('list.txt', 'r'): myURL=don myURL=myURL.replace("rep.php","") print(myURL+"test.php") 

It's printing

http://www.blabla.com blablabla.html 
3
  • Hi and welcome to SO. Your example is not really complete as you print(url) but we don't see where that variable url is defined. Commented Feb 12, 2022 at 15:22
  • 1
    Please include the input data. There is no way that script produces the output shown. We should see "test.php" in there either as a separate line or as the end of the last line. Commented Feb 12, 2022 at 16:00
  • 1
    Sometimes printing data helps. If you add print("DEBUG", repr(myURL), repr(myURL.replace("rep.php","")) you'll see the problem. Commented Feb 12, 2022 at 16:14

1 Answer 1

2

When you iterate over a file handle, you get one line at a time with any trailing newline characters. So, the line is probably 'http://www.blabla.com\n'. That's why, when you append 'blabla.html', the result is split over two lines.

An easy fix is to do

print(myURL.rstrip() + 'blabla.html') 

On a side note, it's good practice to manage resources like file handles with context blocks so that the file handle is explicitly closed (and not left to the garbage collector):

with open('list.txt', 'r') as f: for don in f: ... 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.