A
A
artloveyou2021-12-25 15:32:44
Python
artloveyou, 2021-12-25 15:32:44

Why does concatenation wrap part of the string?

with open('img.log', 'r') as f:
    contents = f.readlines()

for line in contents:
    img = line[45:]
    print(img + '.png')


at the output I get
1
.png
2
.png
3
.png


and it would be necessary for further work
1.png
2.png
3.png


otherwise the files are not found

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-12-25
@artloveyou

Because the lines contain a line break character. Call .strip()to remove these characters. Or immediately read the file differently, for example

with open('img.log', 'r') as f:
    contents = f.read().splitlines()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question