BabaSvoloch
BabaSvoloch

Reputation: 311

Python: printing ascii art from text file, backslashes are being doubled

I made some ascii art in notepad, saved it as a .txt file, and then I used rsync to throw it on a remote server. I wrote a simple python script in the same directory to just echo the contents of the file, like this:

#!/usr/bin/python3
with open('ascii_art.txt', 'rb') as f:
    for line in f:
        print(line)
f.close()

Here's my problem: every time the script encounters a '\' character (which is just part of the ascii art), it prints it twice.

I get that the backslash is an escape character in python, but I don't understand how to get it to not do this. I tried changing one of the backslashes in the text file to a double backslash, thinking it might undo it, but it gave me 4 backslashes instead.

On top of that, at the end of every line in the ascii txt file, the script actually prints out '\r\n' . I'm not sure how to get rid of those.

Anyone have any thoughts on this? Thanks

Upvotes: 0

Views: 2726

Answers (1)

G_M
G_M

Reputation: 3372

I think it might be because of the file mode being rb instead of r but it's really a guess until you post the actual ascii_art.txt:

with open('ascii_art.txt', 'r') as f:
    for line in f:
        print(line.rstrip())

Upvotes: 4

Related Questions