I/O系统有一系列的层次构建而成
下面是操作一个文本文件的例子来查看这种层次
>>> f = open('sample.txt','w')>>> f<_io.TextIOWrapper name='sample.txt' mode='w' encoding='UTF-8'>>>> f.buffer<_io.BufferedWriter name='sample.txt'>>>> f.buffer.raw<_io.FileIO name='sample.txt' mode='wb'>>>>
io.TextIOWrapper 是一个编码和解码Unicode 的文本处理层,io.BufferedWriter 是一个处理二进制数据的带缓冲的I/O 层,io.FileIO 是一个表示操作系统底层文件描述符的原始文件,增加或改变文本编码会涉及增加或改变最上面的io.TextIOWrapper 层
一般来讲,像上面例子这样通过访问属性值来直接操作不同的层是很不安全的,如果你试着使用下面这样的技术改变编码看看会发生什么
>>> f<_io.TextIOWrapper name='sample.txt' mode='w' encoding='UTF-8'>>>> f = io.TextIOWrapper(f.buffer, encoding='latin-1')>>> f<_io.TextIOWrapper name='sample.txt' encoding='latin-1'>>>> f.write('Hello')Traceback (most recent call last):File "", line 1, in ValueError: I/O operation on closed file.>>>
结果出错了,因为f 的原始值已经被破坏了并关闭了底层的文件,
detach() 方法会断开文件的最顶层并返回第二层,之后最顶层就没什么用了
>>> f = open('sample.txt', 'w')>>> f<_io.TextIOWrapper name='sample.txt' mode='w' encoding='UTF-8'>>>> b = f.detach()>>> b<_io.BufferedWriter name='sample.txt'>>>> f.write('hello')Traceback (most recent call last):File "", line 1, in ValueError:underlying buffer has been detached
一旦断开最顶层后,你就可以给返回结果添加一个新的最顶层
>>> f = io.TextIOWrapper(b, encoding='latin-1')>>> f<_io.TextIOWrapper name='sample.txt' encoding='latin-1'>>>>