Python Language Incompatibilities moving from Python 2 to Python 3 File I/O

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

file is no longer a builtin name in 3.x (open still works).

Internal details of file I/O have been moved to the standard library io module, which is also the new home of StringIO:

import io
assert io.open is open # the builtin is an alias
buffer = io.StringIO()
buffer.write('hello, ') # returns number of characters written
buffer.write('world!\n')
buffer.getvalue() # 'hello, world!\n'

The file mode (text vs binary) now determines the type of data produced by reading a file (and type required for writing):

with open('data.txt') as f:
    first_line = next(f)
    assert type(first_line) is str
with open('data.bin', 'rb') as f:
    first_kb = f.read(1024)
    assert type(first_kb) is bytes

The encoding for text files defaults to whatever is returned by locale.getpreferredencoding(False). To specify an encoding explicitly, use the encoding keyword parameter:

with open('old_japanese_poetry.txt', 'shift_jis') as text:
    haiku = text.read()


Got any Python Language Question?