#!/usr/bin/env python3.0 import os import pty for pipe_func, name in ((os.pipe, 'os.pipe'), (pty.openpty, 'pty.openpty')): master_fd, slave_fd = pipe_func() master_file = os.fdopen(master_fd, 'rb') # 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 = master_file.read(len(data) + 1) print(gotdata) master_file.close()