1

I want to be able to read a file and add a string onto it line by line without having a line break in the middle.

text_file = open("read_it.txt", "r")
print text_file.readline() + "0123456789"

Outputs to

>>
text
0123456789
>>

I would like have it output to this format

>>
text0123456789
>>

1 Answer 1

6

Use the rstrip method:

text_file.readline().rstrip() + "0123456789"
Sign up to request clarification or add additional context in comments.

4 Comments

+1, but be aware that not only (CR)LF but also other whitespace will be stripped off the end of the line.
@TimPietzcker Of course you are right. But you can use text.rstrip("\r\n") instead to only strip newlines.
Oh, yes, you're right. Sorry about that. Well, you already have my +1 (but still, \n is enough, even on Windows).
How about text.rstrip(os.linesep) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.