io
os — 杂项操作系统接口
os
源代码: Lib/io.py
The io 模块为 Python 处理各种类型 I/O 提供主要设施。有 3 种主要 I/O 类型: 文本 I/O , 二进制 I/O and 原生 I/O 。这些是一般类别,且它们中的每个都可以用于各种后备存储。属于这些类别的任何具体对象称为 文件对象 。其它常见术语是 stream and 像文件对象 .
独立于类别,每个具体流对象还有各种能力:可以只读、只写或读写。还允许任意随机访问 (向前或向后寻址到任何位置) 或仅顺序访问 (例如:在套接字或管道情况下)。
所有流都关切赋予给它们的有关数据类型。例如,赋予 str 对象到 write() 方法对于二进制流将引发 TypeError 。同样,赋予 bytes 对象到 write() 方法对于文本流。
str
write()
TypeError
bytes
3.3 版改变: 操作用于引发 IOError 现在引发 OSError ,由于 IOError 现在是别名化的 OSError .
IOError
OSError
Text I/O 期望并产生 str 对象。这意味着每当后备存储是本机字节 (譬如:在文件情况下) 时,数据的编码和解码是透明的,还能可选翻译特定平台换行符。
创建文本流的最轻松方式是采用 open() ,可选指定编码:
open()
f = open("myfile.txt", "r", encoding="utf-8")
内存文本流还可用作 StringIO 对象:
StringIO
f = io.StringIO("some initial text data")
文本流 API 的详细描述在文档编制 TextIOBase .
TextIOBase
二进制 I/O (也称 缓冲 I/O ) 期望 像字节对象 并产生 bytes 对象。不履行编码、解码或翻译换行符。这种类别的流可以用于所有种类的非文本数据,且当期望手动控制文本数据的处理时也如此。
创建二进制流的最简单方式是采用 open() with 'b' 在模式字符串下:
'b'
f = open("myfile.jpg", "rb")
内存二进制流还可用作 BytesIO 对象:
BytesIO
f = io.BytesIO(b"some initial binary data: \x00\x01")
二进制流 API 的详细描述在文档化的 BufferedIOBase .
BufferedIOBase
其它库模块可以提供创建文本 (或二进制) 流的额外方式。见 socket.socket.makefile() 例如。
socket.socket.makefile()
原生 I/O (也称 无缓冲 I/O ) 通常用作二进制和文本流的低级构建块;直接操纵来自用户代码的原生流很少有用。尽管如此,可以通过在禁用缓冲的二进制模式下打开文件,创建原生流:
f = open("myfile.jpg", "rb", buffering=0)
原生流 API 的详细描述在文档化的 RawIOBase .
RawIOBase
默认编码对于 TextIOWrapper and open() 是特定区域设置 ( locale.getencoding() ).
TextIOWrapper
locale.getencoding()
However, many developers forget to specify the encoding when opening text files encoded in UTF-8 (e.g. JSON, TOML, Markdown, etc…) since most Unix platforms use UTF-8 locale by default. This causes bugs because the locale encoding is not UTF-8 for most Windows users. For example:
# May not work on Windows when non-ASCII characters in the file. with open("README.md") as f: long_description = f.read()
Accordingly, it is highly recommended that you specify the encoding explicitly when opening text files. If you want to use UTF-8, pass encoding="utf-8" . To use the current locale encoding, encoding="locale" is supported since Python 3.10.
encoding="utf-8"
encoding="locale"
另请参阅
Python UTF-8 Mode can be used to change the default encoding to UTF-8 from locale-specific encoding.
Python 3.15 将使 Python UTF-8 模式 默认。
Added in version 3.10: 见 PEP 597 了解更多细节。
To find where the default locale encoding is used, you can enable the -X warn_default_encoding command line option or set the PYTHONWARNDEFAULTENCODING environment variable, which will emit an EncodingWarning when the default encoding is used.
-X warn_default_encoding
PYTHONWARNDEFAULTENCODING
EncodingWarning
If you are providing an API that uses open() or TextIOWrapper and passes encoding=None as a parameter, you can use text_encoding() so that callers of the API will emit an EncodingWarning if they don’t pass an encoding . However, please consider using UTF-8 by default (i.e. encoding="utf-8" ) for new APIs.
encoding=None
text_encoding()
encoding
包含用于模块缓冲 I/O 类的默认缓冲大小的 int。 open() 使用文件 blksize (如获得通过 os.stat() ) 若可能的话。
os.stat()
这是别名化的内置 open() 函数。
此函数引发 审计事件 open 采用自变量 path , mode and flags 。 mode and flags 自变量可能已被修改 (或推断自原始调用)。
open
打开提供文件按模式 'rb' 。应使用此函数,当意图将内容视为可执行代码时。
'rb'
path 应该为 str 和绝对路径。
可能覆写此函数的行为由早期调用 PyFile_SetOpenCodeHook() 。不管怎样,假定 path 是 str 和绝对路径, open_code(path) 行为应始终如同 open(path, 'rb') 。覆写行为旨在额外验证 (或预处理) 文件。
PyFile_SetOpenCodeHook()
open_code(path)
open(path, 'rb')
Added in version 3.8.
这是帮手函数适于可调用使用 open() or TextIOWrapper 且拥有 encoding=None 参数。
此函数返回 encoding 若它不为 None 。否则,它返回 "locale" or "utf-8" 从属 UTF-8 模式 .
None
"locale"
"utf-8"
此函数发射 EncodingWarning if sys.flags.warn_default_encoding 为 True 和 encoding is None . stacklevel 指定在哪里发射警告。例如:
sys.flags.warn_default_encoding
def read_text(path, encoding=None): encoding = io.text_encoding(encoding) # stacklevel=2 with open(path, encoding) as f: return f.read()
在此范例中, EncodingWarning 被发射对于调用者的 read_text() .
read_text()
见 文本编码 了解更多信息。
Added in version 3.10.
3.11 版改变: text_encoding() 返回 utf-8 当 UTF-8 模式被启用且 encoding is None .
这是兼容性别名对于内置 BlockingIOError 异常。
BlockingIOError
异常继承 OSError and ValueError 这被引发,当在流上调用不支持操作时。
ValueError
sys
包含标准 IO 流: sys.stdin , sys.stdout ,和 sys.stderr .
sys.stdin
sys.stdout
sys.stderr
I/O 流实现的组织是按类层次结构。首先 抽象基类 (ABC) 用于指定流的各种类别,然后具体类提供标准流实现。
注意
ABC (抽象基类) 还提供了一些方法的默认实现,以帮助实现具体的流类。例如, BufferedIOBase 提供实现未优化的 readinto() and readline() .
readinto()
readline()
At the top of the I/O hierarchy is the abstract base class IOBase . It defines the basic interface to a stream. Note, however, that there is no separation between reading and writing to streams; implementations are allowed to raise UnsupportedOperation 若它们不支持给定操作。
IOBase
UnsupportedOperation
The RawIOBase ABC (抽象基类) 延伸 IOBase . It deals with the reading and writing of bytes to a stream. FileIO 子类 RawIOBase to provide an interface to files in the machine’s file system.
FileIO
The BufferedIOBase ABC (抽象基类) 延伸 IOBase . It deals with buffering on a raw binary stream ( RawIOBase )。其子类, BufferedWriter , BufferedReader ,和 BufferedRWPair buffer raw binary streams that are writable, readable, and both readable and writable, respectively. BufferedRandom provides a buffered interface to seekable streams. Another BufferedIOBase 子类, BytesIO , is a stream of in-memory bytes.
BufferedWriter
BufferedReader
BufferedRWPair
BufferedRandom
The TextIOBase ABC (抽象基类) 延伸 IOBase . It deals with streams whose bytes represent text, and handles encoding and decoding to and from strings. TextIOWrapper , which extends TextIOBase , is a buffered text interface to a buffered raw stream ( BufferedIOBase ). Finally, StringIO 是用于文本的内存流。
Argument names are not part of the specification, and only the arguments of open() are intended to be used as keyword arguments.
下表汇总的 ABC (抽象基类) 提供通过 io 模块:
ABC (抽象基类)
继承
Stub 方法
混合方法和特性
fileno , seek ,和 truncate
fileno
seek
truncate
close , closed , __enter__ , __exit__ , flush , isatty , __iter__ , __next__ , readable , readline , readlines , seekable , tell , writable ,和 writelines
close
closed
__enter__
__exit__
flush
isatty
__iter__
__next__
readable
readline
readlines
seekable
tell
writable
writelines
readinto and write
readinto
write
继承 IOBase 方法, read ,和 readall
read
readall
detach , read , read1 ,和 write
detach
read1
继承 IOBase 方法, readinto ,和 readinto1
readinto1
detach , read , readline ,和 write
继承 IOBase 方法, encoding , errors ,和 newlines
errors
newlines
所有 I/O 类的 ABC (抽象基类)。
此类为派生类可以选择性覆盖的很多方法提供空抽象实现;默认实现表示无法读取、写入或寻址的文件。
即使 IOBase 不声明 read() or write() 因为它们的签名有所不同,所以实现和客户端应将这些方法考虑成接口的一部分。此外,实现可能引发 ValueError (或 UnsupportedOperation ) 当调用它们不支持的操作时。
read()
用于从文件读取 (或写入) 二进制数据的基本类型是 bytes 。其它 像字节对象 还被接受作为方法自变量。文本 I/O 类工作采用 str 数据。
注意,调用关闭流的任何方法 (甚至质问) 都是未定义的。实现可能引发 ValueError 在此情况下。
IOBase (及其子类) 支持迭代器协议,意味着 IOBase 对象可以迭代流中产生的行。行的定义有点差异从属流是二进制流 (产生字节) 或文本流 (产生字符串)。见 readline() 下文。
IOBase 也是上下文管理器,因此支持 with 语句。在此范例中, file 被关闭后于 with 语句套件的完成 — 即使出现异常:
with
with open('spam.txt', 'w') as file: file.write('Spam and eggs!')
IOBase 提供这些数据属性和方法:
刷新并关闭该流。此方法不起作用,若文件已关闭。文件一旦被关闭,对文件的任何操作 (如:读取或写入) 都将引发 ValueError .
为了方便,允许多次调用此方法;不管怎样,仅第一次调用有效。
True 若流被关闭。
True
返回流的底层文件描述符 (整数) 若存在。 OSError 被引发若 IO 对象不使用文件描述符。
刷新流的写入缓冲,若适用。这什么都不做,对于只读和非阻塞流。
返回 True 若流可交互 (即:连接到终端/tty 设备)。
返回 True 若流可以被读取。若 False , read() 会引发 OSError .
False
从流读取并返回一行。若 size 有指定,最多 size 字节将被读取。
行终止符始终是 b'\n' 对于二进制文件;对于文本文件, newline 自变量对于 open() 可以用于选择识别行终止符。
b'\n'
从流读取并返回行列表。 hint 可以指定要控制的读取行数:没有更多行将被读取,若到目前为止的所有行总大小 (以字节/字符为单位) 超过 hint .
hint 值为 0 或小于,及 None ,视为没有提示。
0
注意,迭代文件对象已经是可能的使用 for line in file: ... 不调用 file.readlines() .
for line in file: ...
file.readlines()
将流位置改为给定字节 offset , interpreted relative to the position indicated by whence , and return the new absolute position. Values for whence 是:
os.SEEK_SET or 0 – 流的开头 (默认); offset 应该为 0 或正值
os.SEEK_SET
os.SEEK_CUR or 1 – 当前流位置; offset 可能为负值
os.SEEK_CUR
1
os.SEEK_END or 2 – 流末尾; offset 通常为负值
os.SEEK_END
2
Added in version 3.1: The SEEK_* 常量。
SEEK_*
Added in version 3.3: 某些操作系统可以支持额外值,像 os.SEEK_HOLE or os.SEEK_DATA 。文件有效值从属它是以文本模式打开,还是以二进制模式打开。
os.SEEK_HOLE
os.SEEK_DATA
返回 True 若流支持随机访问。若 False , seek() , tell() and truncate() 会引发 OSError .
seek()
tell()
truncate()
返回当前流的位置。
重置流大小到给定 size 以字节为单位 (或当前位置若 size 不指定)。当前流位置不改变。这种重置大小可以扩展 (或缩减) 当前文件的大小。在扩展情况下,新文件区域的内容从属平台 (在大多数系统,额外字节以 0 填充)。返回新的文件大小。
3.5 版改变: Windows 现在以 0 填充文件,当扩展时。
返回 True 若流支持写入。若 False , write() and truncate() 会引发 OSError .
Write a list of lines to the stream. Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.
准备销毁对象。 IOBase 提供此方法的默认实现,调用实例的 close() 方法。
close()
Base class for raw binary streams. It inherits from IOBase .
原生二进制流通常提供对底层 OS 设备或 API 的低级访问,且不会试着将它封装在高级原语中 (这种功能在更高级缓冲二进制流和文本流中完成,本页面稍后会描述)。
RawIOBase 提供这些方法,除了那些来自 IOBase :
读取直到 size 字节从对象并返回它们。为了方便,若 size 未指定或为 -1,返回所有字节直到 EOF (文件末尾)。否则,只做一次系统调用。少于 size 字节也可能返回若操作系统调用返回小于 size 字节。
若返回 0 字节,和 size 非 0,这指示 EOF (文件末尾)。若对象处于非阻塞模式且没有可用字节, None 被返回。
遵从默认实现对于 readall() and readinto() .
readall()
读取并返回来自流的所有字节直到 EOF (文件末尾),使用多次流调用若有必要。
将字节读入预分配,可写 像字节对象 b , and return the number of bytes read. For example, b 可以是 bytearray . If the object is in non-blocking mode and no bytes are available, None 被返回。
bytearray
写入给定 像字节对象 , b , to the underlying raw stream, and return the number of bytes written. This can be less than the length of b in bytes, depending on specifics of the underlying raw stream, and especially if it is in non-blocking mode. None is returned if the raw stream is set not to block and no single byte could be readily written to it. The caller may release or mutate b after this method returns, so the implementation should only access b 在方法调用期间。
Base class for binary streams that support some kind of buffering. It inherits from IOBase .
主要差异相比 RawIOBase 是方法 read() , readinto() and write() 将 (分别) 试着按请求读取尽可能多的输入 (或消耗所有给定输出),以做出或许不止一次的系统调用为代价。
此外,这些方法会引发 BlockingIOError 若底层原生流处于非阻塞模式下且无法获得 (或给出) 足够数据;不像它们的 RawIOBase 搭档,他们从不会返回 None .
此外, read() 方法没有遵从默认实现对于 readinto() .
典型 BufferedIOBase 实现不应继承自 RawIOBase 实现,但包裹某个,像 BufferedWriter and BufferedReader 做的。
BufferedIOBase 提供 (或覆写) 了这些数据属性和方法,除了那些来自 IOBase :
底层原生流 ( RawIOBase 实例) BufferedIOBase 的处理。这不属于 BufferedIOBase API 且在某些实现中可能不存在。
从缓冲分离底层原生流并返回它。
在原生流被分离后,缓冲处于不可用状态。
某些缓冲,像 BytesIO , do not have the concept of a single raw stream to return from this method. They raise UnsupportedOperation .
Added in version 3.1.
读取并返回直到 size bytes. If the argument is omitted, None , or negative, data is read and returned until EOF is reached. An empty bytes object is returned if the stream is already at EOF.
If the argument is positive, and the underlying raw stream is not interactive, multiple raw reads may be issued to satisfy the byte count (unless EOF is reached first). But for interactive raw streams, at most one raw read will be issued, and a short result does not imply that EOF is imminent.
A BlockingIOError is raised if the underlying raw stream is in non blocking-mode, and has no data available at the moment.
读取并返回直到 size bytes, with at most one call to the underlying raw stream’s read() (或 readinto() ) method. This can be useful if you are implementing your own buffering on top of a BufferedIOBase 对象。
若 size is -1 (the default), an arbitrary number of bytes are returned (more than zero unless EOF is reached).
-1
将字节读入预分配,可写 像字节对象 b and return the number of bytes read. For example, b 可以是 bytearray .
像 read() , multiple reads may be issued to the underlying raw stream, unless the latter is interactive.
将字节读入预分配,可写 像字节对象 b , using at most one call to the underlying raw stream’s read() (或 readinto() ) method. Return the number of bytes read.
Added in version 3.5.
写入给定 像字节对象 , b , and return the number of bytes written (always equal to the length of b in bytes, since if the write fails an OSError will be raised). Depending on the actual implementation, these bytes may be readily written to the underlying stream, or held in a buffer for performance and latency reasons.
当在非阻塞模式下时, BlockingIOError is raised if the data needed to be written to the raw stream but it couldn’t accept all the data without blocking.
The caller may release or mutate b after this method returns, so the implementation should only access b 在方法调用期间。
A raw binary stream representing an OS-level file containing bytes data. It inherits from RawIOBase .
The name 可以是 2 件事之一:
字符串或 bytes object representing the path to the file which will be opened. In this case closefd must be True (默认),否则,会引发错误。
an integer representing the number of an existing OS-level file descriptor to which the resulting FileIO object will give access. When the FileIO object is closed this fd will be closed as well, unless closefd 被设为 False .
The mode 可以是 'r' , 'w' , 'x' or 'a' for reading (default), writing, exclusive creation or appending. The file will be created if it doesn’t exist when opened for writing or appending; it will be truncated when opened for writing. FileExistsError will be raised if it already exists when opened for creating. Opening a file for creating implies writing, so this mode behaves in a similar way to 'w' 。添加 '+' to the mode to allow simultaneous reading and writing.
'r'
'w'
'x'
'a'
FileExistsError
'+'
The read() (when called with a positive argument), readinto() and write() methods on this class will only make one system call.
自定义开启器可以用于传递可调用如 opener 。然后,获得文件对象底层文件描述符通过调用 opener 采用 ( name , flags ). opener 必须返回打开文件描述符 (传递 os.open as opener 导致功能类似于传递 None ).
os.open
新近创建的文件 不可继承 .
见 open() 内置函数,例如使用 opener 参数。
3.3 版改变: The opener 参数被添加。 'x' 模式被添加。
3.4 版改变: 文件现在不可继承。
FileIO 提供这些数据属性,除了那些来自 RawIOBase and IOBase :
在构造函数中给定的模式。
文件名。这是文件的文件描述符当构造函数中未给定名称时。
缓冲 I/O 流为 I/O 设备提供更高级接口,相比原生 I/O。
A binary stream using an in-memory bytes buffer. It inherits from BufferedIOBase 。丢弃缓冲当 close() 方法被调用。
可选自变量 initial_bytes 是 像字节对象 包含初始数据。
BytesIO 提供或覆盖这些方法,除了那些来自 BufferedIOBase and IOBase :
返回可读/可写视图,在不拷贝缓冲内容的情况下。还有,变异视图会透明地更新缓冲内容:
>>> b = io.BytesIO(b"abcdef") >>> view = b.getbuffer() >>> view[2:4] = b"56" >>> b.getvalue() b'ab56ef'
只要视图存在, BytesIO 对象不可以重置大小或关闭。
Added in version 3.2.
返回 bytes 包含缓冲的整个内容。
在 BytesIO ,这如同 read() .
3.7 版改变: The size 自变量现在是可选的。
在 BytesIO ,这如同 readinto() .
缓冲二进制流提供更高级访问对可读、不可寻址 RawIOBase raw binary stream. It inherits from BufferedIOBase .
当从此对象读取数据时,从底层原生流请求的数据量可能更大,并保持在内部缓冲。然后,后续读取时可以直接返回缓冲数据。
构造函数创建 BufferedReader 对于给定可读 raw 流和 buffer_size 。若 buffer_size 被省略, DEFAULT_BUFFER_SIZE 被使用。
DEFAULT_BUFFER_SIZE
BufferedReader 提供或覆盖这些方法,除了那些来自 BufferedIOBase and IOBase :
Return bytes from the stream without advancing the position. At most one single read on the raw stream is done to satisfy the call. The number of bytes returned may be less or more than requested.
读取并返回 size bytes, or if size is not given or negative, until EOF or if the read call would block in non-blocking mode.
读取并返回直到 size bytes with only one call on the raw stream. If at least one byte is buffered, only buffered bytes are returned. Otherwise, one raw stream read call is made.
A buffered binary stream providing higher-level access to a writeable, non seekable RawIOBase raw binary stream. It inherits from BufferedIOBase .
When writing to this object, data is normally placed into an internal buffer. The buffer will be written out to the underlying RawIOBase object under various conditions, including:
when the buffer gets too small for all pending data;
当 flush() 被调用;
flush()
当 seek() 被请求 (对于 BufferedRandom 对象);
当 BufferedWriter 对象被关闭 (或销毁)。
构造函数创建 BufferedWriter for the given writeable raw stream. If the buffer_size 不给定,默认为 DEFAULT_BUFFER_SIZE .
BufferedWriter 提供或覆盖这些方法,除了那些来自 BufferedIOBase and IOBase :
Force bytes held in the buffer into the raw stream. A BlockingIOError should be raised if the raw stream blocks.
写入 像字节对象 , b ,并返回写入字节数。当在非阻塞模式下, BlockingIOError 被引发若需要写出缓冲,但原生流阻塞。
A buffered binary stream providing higher-level access to a seekable RawIOBase raw binary stream. It inherits from BufferedReader and BufferedWriter .
The constructor creates a reader and writer for a seekable raw stream, given in the first argument. If the buffer_size is omitted it defaults to DEFAULT_BUFFER_SIZE .
BufferedRandom is capable of anything BufferedReader or BufferedWriter can do. In addition, seek() and tell() are guaranteed to be implemented.
A buffered binary stream providing higher-level access to two non seekable RawIOBase raw binary streams—one readable, the other writeable. It inherits from BufferedIOBase .
reader and writer are RawIOBase objects that are readable and writeable respectively. If the buffer_size is omitted it defaults to DEFAULT_BUFFER_SIZE .
BufferedRWPair 实现所有的 BufferedIOBase 方法除了 detach() ,其引发 UnsupportedOperation .
detach()
警告
BufferedRWPair does not attempt to synchronize accesses to its underlying raw streams. You should not pass it the same object as reader and writer; use BufferedRandom 代替。
Base class for text streams. This class provides a character and line based interface to stream I/O. It inherits from IOBase .
TextIOBase 提供 (或覆写) 了这些数据属性和方法,除了那些来自 IOBase :
用于将流字节解码成字符串,和将字符串编码成字节的编码名称。
解码器 (或编码器) 的错误设置。
字符串、字符串元组、或 None ,指示到目前为止翻译的换行符。从属实现和初始构造函数标志,这可能不可用。
底层二进制缓冲 ( BufferedIOBase 实例) TextIOBase 的处理。这不属于 TextIOBase API 且在某些实现中可能不存在。
分隔底层二进制缓冲从 TextIOBase 并返回它。
分离底层缓冲后, TextIOBase 处于不可用状态。
某些 TextIOBase 实现,像 StringIO ,可能没有底层缓冲概念且调用此方法会引发 UnsupportedOperation .
读取并返回最多 size 字符从流作为单 str 。若 size 为负或者 None ,读取直到 EOF (文件末尾)。
读取直到换行符或 EOF (文件末尾) 并返回单 str 。若流已在 EOF (文件末尾),返回空字符串。
若 size 有指定,最多 size 字符将被读取。
Change the stream position to the given offset . Behaviour depends on the whence parameter. The default value for whence is SEEK_SET .
SEEK_SET
SEEK_SET or 0 : seek from the start of the stream (the default); offset must either be a number returned by TextIOBase.tell() , or zero. Any other offset value produces undefined behaviour.
TextIOBase.tell()
SEEK_CUR or 1 : “seek” to the current position; offset must be zero, which is a no-operation (all other values are unsupported).
SEEK_CUR
SEEK_END or 2 : seek to the end of the stream; offset must be zero (all other values are unsupported).
SEEK_END
以不透明数字形式返回新的绝对位置。
Return the current stream position as an opaque number. The number does not usually represent a number of bytes in the underlying binary storage.
写入字符串 s 到流并返回写入字符数。
缓冲文本流提供更高级访问对 BufferedIOBase buffered binary stream. It inherits from TextIOBase .
encoding gives the name of the encoding that the stream will be decoded or encoded with. It defaults to locale.getencoding() . encoding="locale" can be used to specify the current locale’s encoding explicitly. See 文本编码 了解更多信息。
errors is an optional string that specifies how encoding and decoding errors are to be handled. Pass 'strict' 会引发 ValueError exception if there is an encoding error (the default of None has the same effect), or pass 'ignore' to ignore errors. (Note that ignoring encoding errors can lead to data loss.) 'replace' 导致置换标记 (譬如 '?' ) 被插入在畸形数据处。 'backslashreplace' causes malformed data to be replaced by a backslashed escape sequence. When writing, 'xmlcharrefreplace' (replace with the appropriate XML character reference) or 'namereplace' (replace with \N{...} escape sequences) can be used. Any other error handling name that has been registered with codecs.register_error() is also valid.
'strict'
'ignore'
'replace'
'?'
'backslashreplace'
'xmlcharrefreplace'
'namereplace'
\N{...}
codecs.register_error()
newline controls how line endings are handled. It can be None , '' , '\n' , '\r' ,和 '\r\n' 。其工作如下:
''
'\n'
'\r'
'\r\n'
当从流读取输入时,若 newline is None , 通用换行符 mode is enabled. Lines in the input can end in '\n' , '\r' ,或 '\r\n' ,且这些会被翻译成 '\n' before being returned to the caller. If newline is '' , universal newlines mode is enabled, but line endings are returned to the caller untranslated. If newline has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.
当写入输出到流时,若 newline is None ,任何 '\n' 写入字符被翻译成系统默认行分隔符, os.linesep 。若 newline is '' or '\n' ,不发生翻译。若 newline 是任何其它合法值,任何 '\n' 写入字符被翻译成给定字符串。
os.linesep
若 line_buffering is True , flush() is implied when a call to write contains a newline character or a carriage return.
若 write_through is True ,调用 write() are guaranteed not to be buffered: any data written on the TextIOWrapper object is immediately handled to its underlying binary buffer .
3.3 版改变: The write_through 自变量被添加。
3.3 版改变: 默认 encoding 现为 locale.getpreferredencoding(False) 而不是 locale.getpreferredencoding() . Don’t change temporary the locale encoding using locale.setlocale() , use the current locale encoding instead of the user preferred encoding.
locale.getpreferredencoding(False)
locale.getpreferredencoding()
locale.setlocale()
3.10 版改变: The encoding 自变量现在支持 "locale" 虚设编码名称。
TextIOWrapper 提供这些数据属性和方法,除了那些来自 TextIOBase and IOBase :
行缓冲是否被启用。
Whether writes are passed immediately to the underlying binary buffer.
Added in version 3.7.
重新配置此文本流使用新设置为 encoding , errors , newline , line_buffering and write_through .
未指定参数保持当前设置,除了 errors='strict' 的使用当 encoding 有指定但 errors 未指定。
errors='strict'
改变 encoding 或 newline 不可能,若已从流读取了一些数据。另一方面,写入后改变 encoding 是可能的。
此方法做隐式流刷新,在设置新参数前。
3.11 版改变: 方法支持 encoding="locale" 选项。
Set the stream position. Return the new stream position as an int .
int
Four operations are supported, given by the following argument combinations:
seek(0, SEEK_SET) : Rewind to the start of the stream.
seek(0, SEEK_SET)
seek(cookie, SEEK_SET) : Restore a previous position; cookie 必须为 a number returned by tell() .
seek(cookie, SEEK_SET)
seek(0, SEEK_END) : Fast-forward to the end of the stream.
seek(0, SEEK_END)
seek(0, SEEK_CUR) : Leave the current stream position unchanged.
seek(0, SEEK_CUR)
Any other argument combinations are invalid, and may raise exceptions.
os.SEEK_SET , os.SEEK_CUR ,和 os.SEEK_END .
Return the stream position as an opaque number. The return value of tell() can be given as input to seek() , to restore a previous stream position.
A text stream using an in-memory text buffer. It inherits from TextIOBase .
文本缓冲被丢弃当 close() 方法被调用。
The initial value of the buffer can be set by providing initial_value . If newline translation is enabled, newlines will be encoded as if by write() . The stream is positioned at the start of the buffer which emulates opening an existing file in a w+ mode, making it ready for an immediate write from the beginning or for a write that would overwrite the initial value. To emulate opening a file in an a+ mode ready for appending, use f.seek(0, io.SEEK_END) to reposition the stream at the end of the buffer.
w+
a+
f.seek(0, io.SEEK_END)
The newline 自变量的工作像 TextIOWrapper , except that when writing output to the stream, if newline is None , newlines are written as \n 在所有平台。
\n
StringIO 提供此方法,除了那些来自 TextIOBase and IOBase :
返回 str containing the entire contents of the buffer. Newlines are decoded as if by read() , although the stream position is not changed.
用法范例:
import io output = io.StringIO() output.write('First line.\n') print('Second line.', file=output) # Retrieve file contents -- this will be # 'First line.\nSecond line.\n' contents = output.getvalue() # Close object and discard memory buffer -- # .getvalue() will now raise an exception. output.close()
解码换行符的帮手编解码器,对于 通用换行符 mode. It inherits from codecs.IncrementalDecoder .
codecs.IncrementalDecoder
本节讨论提供的具体 I/O 实现的性能。
By reading and writing only large chunks of data even when the user asks for a single byte, buffered I/O hides any inefficiency in calling and executing the operating system’s unbuffered I/O routines. The gain depends on the OS and the kind of I/O which is performed. For example, on some modern OSes such as Linux, unbuffered disk I/O can be as fast as buffered I/O. The bottom line, however, is that buffered I/O offers predictable performance regardless of the platform and the backing device. Therefore, it is almost always preferable to use buffered I/O rather than unbuffered I/O for binary data.
Text I/O over a binary storage (such as a file) is significantly slower than binary I/O over the same storage, because it requires conversions between unicode and binary data using a character codec. This can become noticeable handling huge amounts of text data like large log files. Also, tell() and seek() are both quite slow due to the reconstruction algorithm used.
StringIO ,不管怎样,是本机内存 unicode 容器,且显露速度类似 BytesIO .
FileIO 对象在操作系统调用程度是线程安全的 (譬如 read(2) 在 Unix),它们的包裹也是线程安全的。
二进制缓冲对象 (实例化的 BufferedReader , BufferedWriter , BufferedRandom and BufferedRWPair ) protect their internal structures using a lock; it is therefore safe to call them from multiple threads at once.
TextIOWrapper 对象不是线程安全的。
二进制缓冲对象 (实例化的 BufferedReader , BufferedWriter , BufferedRandom and BufferedRWPair ) are not reentrant. While reentrant calls will not happen in normal situations, they can arise from doing I/O in a signal handler. If a thread tries to re-enter a buffered object which it is already accessing, a RuntimeError is raised. Note this doesn’t prohibit a different thread from entering the buffered object.
signal
RuntimeError
The above implicitly extends to text files, since the open() function will wrap a buffered object inside a TextIOWrapper . This includes standard streams and therefore affects the built-in print() function as well.
print()
time — 时间的访问和转换
time
键入搜索术语或模块、类、函数名称。