import os for pipe_func, name in ((os.pipe, 'os.pipe'), (os.openpty, 'os.openpty')): master_fd, slave_fd = pipe_func() # Simulate a subprocess writing some data to the # slave end of the pipe, and then exiting. data = ('%s: success' % name).encode() os.write(slave_fd, data) os.close(slave_fd) gotdata = os.read(master_fd, len(data) + 1) # not enough data print(gotdata) os.read(master_fd, 1) # one more read => OSError(5) os.close(master_fd)