Files opened in a non-binary mode (e.g.
'w') deal with strings. The deafult encoding is
open(fn, mode='r') # opens file for reading in utf8 open(fn, mode='r', encoding='utf16') # opens file for reading utf16 # ERROR: cannot write bytes when a string is expected: open("foo.txt", "w").write(b"foo")
Files opened in a binary mode (e.g.
'wb') deal with bytes. No encoding argument can be specified as there is no encoding.
open(fn, mode='wb') # open file for writing bytes # ERROR: cannot write string when bytes is expected: open(fn, mode='wb').write("hi")