在Python 2中.带有"类似文件"对象的x:
sys.stdout.write(bytes_)
tempfile.TemporaryFile().write(bytes_)
open('filename', 'wb').write(bytes_)
StringIO().write(bytes_)
在Python3中如何做到这一点?
如何编写与Python 2相当的代码.x代码:
def write(file_, bytes_):
file_.write(bytes_)
注意:sys.stdout
在语义上并不总是一个文本流.有时把它看作字节流可能是有益的.例如,make encrypted archive of dir/ on remote machine:
tar -c dir/ | gzip | gpg -c | ssh user@remote 'dd of=dir.tar.gz.gpg'
在这种情况下,使用Unicode没有意义.