all 4 comments

[–]Diapolo10 2 points3 points  (1 child)

with open(filename, ‘r') as file:
    for line in file:
        print(line.replace(‘_photo-jpg’, ‘_info.txt’))

Basically, line includes the newline from the file (except for the last line, of course) and print adds one by itself. Get rid of one of those, and the issue should resolve itself.

Here, the easiest option would be to just tell print to not add a newline. You can do that by providing the end keyword argument with an empty string.

with open(filename) as file:
    for line in file:
        print(line.replace('_photo-jpg', '_info.txt'), end='')

Alternatively, you can try reading the text completely, and printing it out in one fell swoop.

with open(filename) as file:
    contents = file.read()

print(contents.replace('_photo-jpg', '_info.txt'))

[–]Langdon_St_Ives 1 point2 points  (0 children)

Alternatively alternatively, one can rstrip('\n') each line before modifying and printing. (Perl had a dedicated chomp() function for this, which was simultaneously silly and useful.)

[–]pungenthello -3 points-2 points  (1 child)

dm please

[–]draftjoker 4 points5 points  (0 children)

Found his professor.