I'm using Python 2.6 to read latin2 encoded file with windows line endings ('\r\n').
import codecs
file = codecs.open('stackoverflow_secrets.txt', encoding='latin2', mode='rt')
line = file.readline()
print(repr(line))
outputs : u'login: yabcok\n'
file = codecs.open('stackoverflow_secrets.txt', encoding='latin2', mode='r')
line = file.readline()
print(repr(line))
or
file = codecs.open('stackoverflow_secrets.txt', encoding='latin2', mode='rb')
line = file.readline()
print(repr(line))
outputs : u'password: l1x1%Dm\r\n'
My questions:
- Why text mode is not the default? Documentation states otherwise. Is
codecs
module commonly used with binary files? - Why newline chars aren't stripped from readline() output? This is annoying and redundant.
- Is there a way to specify newline character for files not ASCII encoded.