There are two ways to open a text file in Python:
f = open(filename)
And
import codecs
f = codecs.open(filename, encoding=\
Personally, I always use codecs.open
unless there's a clear identified need to use open
**. The reason is that there's been so many times when I've been bitten by having utf-8 input sneak into my programs. "Oh, I just know it'll always be ascii" tends to be an assumption that gets broken often.
Assuming 'utf-8' as the default encoding tends to be a safer default choice in my experience, since ASCII can be treated as UTF-8, but the converse is not true. And in those cases when I truly do know that the input is ASCII, then I still do codecs.open
as I'm a firm believer in "explicit is better than implicit".
** - in Python 2.x, as the comment on the question states in Python 3 open
replaces codecs.open