15.2. io — 用于操控流的核心工具

2.6 版新增。

The io module provides the Python interfaces to stream handling. Under Python 2.x, this is proposed as an alternative to the built-in file object, but in Python 3.x it is the default interface to access files and streams.

注意

Since this module has been designed primarily for Python 3.x, you have to be aware that all uses of “bytes” in this document refer to the str type (of which bytes is an alias), and all uses of “text” refer to the unicode type. Furthermore, those two types are not interchangeable in the io API。

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 an IOError 若它们不支持给定操作。

延伸 IOBase is RawIOBase which deals simply with the reading and writing of raw bytes to a stream. FileIO 子类 RawIOBase to provide an interface to files in the machine’s file system.

BufferedIOBase deals with buffering on a raw byte stream ( RawIOBase )。其子类, BufferedWriter , BufferedReader ,和 BufferedRWPair buffer streams that are readable, writable, and both readable and writable. BufferedRandom provides a buffered interface to random access streams. BytesIO is a simple stream of in-memory bytes.

Another IOBase 子类, TextIOBase , deals with streams whose bytes represent text, and handles encoding and decoding from and to unicode strings. TextIOWrapper , which extends it, is a buffered text interface to a buffered raw stream ( BufferedIOBase ). Finally, StringIO is an in-memory stream for unicode text.

Argument names are not part of the specification, and only the arguments of open() are intended to be used as keyword arguments.

15.2.1. Module Interface

io. DEFAULT_BUFFER_SIZE

包含用于模块缓冲 I/O 类的默认缓冲大小的 int。 open() 使用文件 blksize (如获得通过 os.stat() ) 若可能的话。

io. open ( file , mode='r' , buffering=-1 , encoding=None , errors=None , newline=None , closefd=True )

打开 file and return a corresponding stream. If the file cannot be opened, an IOError 被引发。

file is either a string giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is given, it is closed when the returned I/O object is closed, unless closefd 被设为 False )。

mode 是指定文件打开模式的可选字符串。默认为 'r' 意味着以文本模式打开以供读取。其它常见值 'w' for writing (truncating the file if it already exists), and 'a' 以供追加 (在 some Unix 系统,意味着 all 写入将追加到 EOF 文件末尾,不管当前寻址位置)。在文本模式,若 encoding is not specified the encoding used is platform dependent. (For reading and writing raw bytes use binary mode and leave encoding 不指定)。可用模式包含:

字符 含义
'r' 打开以供读取 (默认)
'w' 打开以供写入,先截取文件
'a' 打开以供写入,追加到 EOF (文件末尾) 若存在
'b' 二进制模式
't' 文本模式 (默认)
'+' 打开磁盘文件为更新 (读写)
'U' universal newlines mode (for backwards compatibility; should not be used in new code)

默认模式为 'rt' (open for reading text). For binary random access, the mode 'w+b' opens and truncates the file to 0 bytes, while 'r+b' 打开文件不截断。

Python distinguishes between files opened in binary and text modes, even when the underlying operating system doesn’t. Files opened in binary mode (including 'b' mode 自变量) 返回内容按 bytes 对象没有任何解码。以文本模式 (默认,或当 't' 包括在 mode 自变量), 返回文件内容是按 unicode strings, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding 若给定。

buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size of a fixed-size chunk buffer. When no buffering 自变量的给定,默认缓冲策略工作如下:

  • Binary files are buffered in fixed-size chunks; the size of the buffer is chosen using a heuristic trying to determine the underlying device’s “block size” and falling back on DEFAULT_BUFFER_SIZE . On many systems, the buffer will typically be 4096 or 8192 bytes long.
  • "交互" 文本文件 (文件 isatty() returns True) use line buffering. Other text files use the policy described above for binary files.

encoding 是用于解码 (或编码) 文件的编码名称。这只应用于文本模式。默认编码从属平台 (不管 locale.getpreferredencoding() returns), but any encoding supported by Python can be used. See the codecs 模块,了解支持的编码列表。

errors is an optional string that specifies how encoding and decoding errors are to be handled–this cannot be used in binary mode. 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' 导致置换标记 (譬如 '?' ) to be inserted where there is malformed data. When writing, 'xmlcharrefreplace' (replace with the appropriate XML character reference) or 'backslashreplace' (replace with backslashed escape sequences) can be used. Any other error handling name that has been registered with codecs.register_error() is also valid.

newline 控制如何 通用换行符 works (it only applies to text mode). It can be None , '' , '\n' , '\r' ,和 '\r\n' 。其工作如下:

  • On input, if newline is None , universal newlines mode is enabled. Lines in the input can end in '\n' , '\r' ,或 '\r\n' , and these are translated into '\n' 在返回给调用者之前。若为 '' , universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it 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.
  • On output, if newline is None ,任何 '\n' characters written are translated to the system default line separator, os.linesep 。若 newline is '' ,不发生翻译。若 newline is any of the other legal values, any '\n' characters written are translated to the given string.

closefd is False 且给定文件描述符而不是文件名,底层文件描述符将保持打开,当关闭文件时。若给定文件名 closefd has no effect and must be True (the default).

The type of file object returned by the open() 函数从属模式。当 open() 被用来打开文件按文本模式 ( 'w' , 'r' , 'wt' , 'rt' ,等),它返回子类化的 TextIOBase (专门 TextIOWrapper )。当采用缓冲按二进制模式用来打开文件,返回类是子类化的 BufferedIOBase . The exact class varies: in read binary mode, it returns a BufferedReader ; in write binary and append binary modes, it returns a BufferedWriter , and in read/write mode, it returns a BufferedRandom 。当缓冲被禁用时,原生流子类化的 RawIOBase , FileIO ,被返回。

It is also possible to use an unicode or bytes string as a file for both reading and writing. For unicode strings StringIO can be used like a file opened in text mode, and for bytes a BytesIO can be used like a file opened in a binary mode.

exception io. BlockingIOError

Error raised when blocking would occur on a non-blocking stream. It inherits IOError .

In addition to those of IOError , BlockingIOError has one attribute:

characters_written

An integer containing the number of characters written to the stream before it blocked.

exception io. UnsupportedOperation

异常继承 IOError and ValueError 这被引发,当在流上调用不支持操作时。

15.2.2. I/O Base Classes

class io. IOBase

所有 I/O 类的抽象基类,作用于字节流。没有公共构造函数。

此类为派生类可以选择性覆盖的很多方法提供空抽象实现;默认实现表示无法读取、写入或寻址的文件。

即使 IOBase 不声明 read() , readinto() ,或 write() 因为它们的签名有所不同,所以实现和客户端应将这些方法考虑成接口的一部分。此外,实现可能引发 IOError when operations they do not support are called.

用于从文件读取 (或写入) 二进制数据的基本类型是 bytes (also known as str ). bytearray s are accepted too, and in some cases (such as readinto ) required. Text I/O classes work with unicode 数据。

注意,调用关闭流的任何方法 (甚至质问) 都是未定义的。实现可能引发 IOError 在此情况下。

IOBase (and its subclasses) support the iterator protocol, meaning that an IOBase object can be iterated over yielding the lines in a stream. Lines are defined slightly differently depending on whether the stream is a binary stream (yielding bytes ), or a text stream (yielding unicode strings). See readline() 下文。

IOBase is also a context manager and therefore supports the with 语句。在此范例中, file 被关闭后于 with 语句套件的完成 — 即使出现异常:

with io.open('spam.txt', 'w') as file:
    file.write(u'Spam and eggs!')
						

IOBase 提供这些数据属性和方法:

close ( )

刷新并关闭该流。此方法不起作用,若文件已关闭。文件一旦被关闭,对文件的任何操作 (如:读取或写入) 都将引发 ValueError .

为了方便,允许多次调用此方法;不管怎样,仅第一次调用有效。

closed

True if the stream is closed.

fileno ( )

返回流的底层文件描述符 (整数) 若存在。 IOError 被引发若 IO 对象不使用文件描述符。

flush ( )

刷新流的写入缓冲,若适用。这什么都不做,对于只读和非阻塞流。

isatty ( )

返回 True 若流可交互 (即:连接到终端/tty 设备)。

readable ( )

返回 True 若流可以被读取。若 False , read() 会引发 IOError .

readline ( limit=-1 )

从流读取并返回一行。若 limit 有指定,最多 limit 字节将被读取。

行终止符始终是 b'\n' 对于二进制文件;对于文本文件, newline 自变量对于 open() 可以用于选择识别行终止符。

readlines ( hint=-1 )

从流读取并返回行列表。 hint 可以指定要控制的读取行数:没有更多行将被读取,若到目前为止的所有行总大小 (以字节/字符为单位) 超过 hint .

注意,迭代文件对象已经是可能的使用 for line in 文件: ... 不调用 file.readlines() .

seek ( offset , whence=SEEK_SET )

将流位置改为给定字节 offset . offset 的解释是相对位置指示通过 whence 。值对于 whence 是:

  • SEEK_SET or 0 – 流的开头 (默认); offset 应该为 0 或正值
  • SEEK_CUR or 1 – 当前流位置; offset may be negative
  • SEEK_END or 2 – 流末尾; offset 通常是 negative

返回新的绝对位置。

New in version 2.7: The SEEK_* constants

seekable ( )

返回 True 若流支持随机访问。若 False , seek() , tell() and truncate() 会引发 IOError .

tell ( )

返回当前流的位置。

truncate ( size=None )

重置流大小到给定 size 以字节为单位 (或当前位置若 size is not specified). The current stream position isn’t changed. This resizing can extend or reduce the current file size. In case of extension, the contents of the new file area depend on the platform (on most systems, additional bytes are zero-filled, on Windows they’re undetermined). The new file size is returned.

writable ( )

返回 True 若流支持写入。若 False , write() and truncate() 会引发 IOError .

writelines ( lines )

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.

__del__ ( )

准备销毁对象。 IOBase 提供此方法的默认实现,调用实例的 close() 方法。

class io. RawIOBase

用于原生二进制 I/O 的基类。它继承 IOBase 。没有公共构造函数。

原生二进制 I/O 通常提供对底层 OS 设备 (或 API) 的低级访问,且不会试着将它封装在高级原语中 (这留给缓冲 I/O 和文本 I/O,本页稍后描述)。

除属性和方法来自 IOBase , RawIOBase provides the following methods:

read ( n=-1 )

读取直到 n 字节从对象并返回它们。为了方便,若 n is unspecified or -1, readall() is called. Otherwise, only one system call is ever made. Fewer than n 字节也可能返回若操作系统调用返回小于 n 字节。

若返回 0 字节,和 n 非 0,这指示 EOF (文件末尾)。若对象处于非阻塞模式且没有可用字节, None 被返回。

readall ( )

读取并返回来自流的所有字节直到 EOF (文件末尾),使用多次流调用若有必要。

readinto ( b )

Read up to len(b) bytes into bytearray b and return the number of bytes read. If the object is in non-blocking mode and no bytes are available, None 被返回。

write ( b )

Write the given bytes or bytearray object, b , to the underlying raw stream and return the number of bytes written. This can be less than len(b) , 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.

class io. BufferedIOBase

用于支持某种缓冲的二进制流的基类。它继承 IOBase 。没有公共构造函数。

主要差异相比 RawIOBase 是方法 read() , readinto() and write() 将 (分别) 试着按请求读取尽可能多的输入 (或消耗所有给定输出),以做出或许不止一次的系统调用为代价。

此外,这些方法会引发 BlockingIOError 若底层原生流处于非阻塞模式下且无法获得 (或给出) 足够数据;不像它们的 RawIOBase 搭档,他们从不会返回 None .

此外, read() 方法没有遵从默认实现对于 readinto() .

典型 BufferedIOBase 实现不应继承自 RawIOBase 实现,但包裹某个,像 BufferedWriter and BufferedReader 做的。

BufferedIOBase 提供 (或覆写) 了这些方法和属性,除了那些来自 IOBase :

raw

底层原生流 ( RawIOBase 实例) BufferedIOBase 的处理。这不属于 BufferedIOBase API 且在某些实现中可能不存在。

detach ( )

从缓冲分离底层原生流并返回它。

在原生流被分离后,缓冲处于不可用状态。

某些缓冲,像 BytesIO , do not have the concept of a single raw stream to return from this method. They raise UnsupportedOperation .

2.7 版新增。

read ( n=-1 )

读取并返回直到 n 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.

read1 ( n=-1 )

读取并返回直到 n bytes, with at most one call to the underlying raw stream’s read() method. This can be useful if you are implementing your own buffering on top of a BufferedIOBase 对象。

readinto ( b )

Read up to len(b) bytes into bytearray b and return the number of bytes read.

read() , multiple reads may be issued to the underlying raw stream, unless the latter is ‘interactive’.

A BlockingIOError is raised if the underlying raw stream is in non blocking-mode, and has no data available at the moment.

write ( b )

Write the given bytes or bytearray object, b and return the number of bytes written (never less than len(b) , since if the write fails an IOError 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.

15.2.3. Raw File I/O

class io. FileIO ( name , mode='r' , closefd=True )

FileIO 表示的 OS 级别文件包含 bytes 数据。它实现了 RawIOBase 接口 (因此 IOBase 接口,也)。

The name 可以是 2 件事之一:

  • a string representing the path to the file which will be opened;
  • an integer representing the number of an existing OS-level file descriptor to which the resulting FileIO object will give access.

The mode 可以是 'r' , 'w' or 'a' for reading (default), writing, 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. Add a '+' to the mode to allow simultaneous reading and writing.

The read() (when called with a positive argument), readinto() and write() methods on this class will only make one system call.

除属性和方法来自 IOBase and RawIOBase , FileIO provides the following data attributes and methods:

mode

在构造函数中给定的模式。

名称

文件名。这是文件的文件描述符当构造函数中未给定名称时。

15.2.4. Buffered Streams

缓冲 I/O 流为 I/O 设备提供更高级接口,相比原生 I/O。

class io. BytesIO ( [ initial_bytes ] )

使用内存 bytes 缓冲实现的流。它继承 BufferedIOBase .

自变量 initial_bytes is an optional initial bytes .

BytesIO 提供或覆盖这些方法,除了那些来自 BufferedIOBase and IOBase :

getvalue ( )

返回 bytes 包含缓冲的整个内容。

read1 ( )

BytesIO ,这如同 read() .

class io. BufferedReader ( raw , buffer_size=DEFAULT_BUFFER_SIZE )

提供高级访问的缓冲,对可读、顺序 RawIOBase 对象。它继承 BufferedIOBase . When reading data from this object, a larger amount of data may be requested from the underlying raw stream, and kept in an internal buffer. The buffered data can then be returned directly on subsequent reads.

构造函数创建 BufferedReader 对于给定可读 raw 流和 buffer_size 。若 buffer_size 被省略, DEFAULT_BUFFER_SIZE 被使用。

BufferedReader 提供或覆盖这些方法,除了那些来自 BufferedIOBase and IOBase :

peek ( [ n ] )

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.

read ( [ n ] )

读取并返回 n bytes, or if n is not given or negative, until EOF or if the read call would block in non-blocking mode.

read1 ( n )

读取并返回直到 n 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.

class io. BufferedWriter ( raw , buffer_size=DEFAULT_BUFFER_SIZE )

A buffer providing higher-level access to a writeable, sequential RawIOBase 对象。它继承 BufferedIOBase . When writing to this object, data is normally held into an internal buffer. The buffer will be written out to the underlying RawIOBase object under various conditions, including:

构造函数创建 BufferedWriter for the given writeable raw stream. If the buffer_size 不给定,默认为 DEFAULT_BUFFER_SIZE .

A third argument, max_buffer_size , is supported, but unused and deprecated.

BufferedWriter 提供或覆盖这些方法,除了那些来自 BufferedIOBase and IOBase :

flush ( )

Force bytes held in the buffer into the raw stream. A BlockingIOError should be raised if the raw stream blocks.

write ( b )

Write the bytes or bytearray object, b and return the number of bytes written. When in non-blocking mode, a BlockingIOError 被引发若需要写出缓冲,但原生流阻塞。

class io. BufferedRandom ( raw , buffer_size=DEFAULT_BUFFER_SIZE )

A buffered interface to random access streams. It inherits BufferedReader and BufferedWriter , and further supports seek() and tell() 功能。

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 .

A third argument, max_buffer_size , is supported, but unused and deprecated.

BufferedRandom is capable of anything BufferedReader or BufferedWriter can do.

class io. BufferedRWPair ( reader , writer , buffer_size=DEFAULT_BUFFER_SIZE )

A buffered I/O object combining two unidirectional RawIOBase objects – one readable, the other writeable – into a single bidirectional endpoint. It inherits 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 .

A fourth argument, max_buffer_size , is supported, but unused and deprecated.

BufferedRWPair 实现所有的 BufferedIOBase ‘s methods except for detach() ,其引发 UnsupportedOperation .

警告

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 代替。

15.2.5. Text I/O

class io. TextIOBase

Base class for text streams. This class provides an unicode character and line based interface to stream I/O. There is no readinto() method because Python’s unicode strings are immutable. It inherits IOBase 。没有公共构造函数。

TextIOBase 提供 (或覆写) 了这些数据属性和方法,除了那些来自 IOBase :

encoding

用于将流字节解码成字符串,和将字符串编码成字节的编码名称。

errors

解码器 (或编码器) 的错误设置。

newlines

字符串、字符串元组、或 None ,指示到目前为止翻译的换行符。从属实现和初始构造函数标志,这可能不可用。

buffer

底层二进制缓冲 ( BufferedIOBase 实例) TextIOBase 的处理。这不属于 TextIOBase API 且在某些实现中可能不存在。

detach ( )

分隔底层二进制缓冲从 TextIOBase 并返回它。

分离底层缓冲后, TextIOBase 处于不可用状态。

某些 TextIOBase 实现,像 StringIO ,可能没有底层缓冲概念且调用此方法会引发 UnsupportedOperation .

2.7 版新增。

read ( n )

读取并返回最多 n 字符从流作为单 unicode 。若 n 为负或者 None ,读取直到 EOF (文件末尾)。

readline ( limit=-1 )

读取直到换行符或 EOF (文件末尾) 并返回单 unicode 。若流已在 EOF (文件末尾),返回空字符串。

limit 有指定,最多 limit 字符将被读取。

seek ( offset , whence=SEEK_SET )

Change the stream position to the given offset . Behaviour depends on the whence parameter:

  • 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 值 produces undefined behaviour.
  • SEEK_CUR or 1 : “seek” to the current position; offset must be zero, which is a no-operation (all other values are unsupported).
  • SEEK_END or 2 : seek to the end of the stream; offset must be zero (all other values are unsupported).

以不透明数字形式返回新的绝对位置。

New in version 2.7: The SEEK_* 常量。

tell ( )

Return the current stream position as an opaque number. The number does not usually represent a number of bytes in the underlying binary storage.

write ( s )

写入 unicode string s 到流并返回写入字符数。

class io. TextIOWrapper ( buffer , encoding=None , errors=None , newline=None , line_buffering=False )

缓冲文本流基于 BufferedIOBase 二进制流。它继承 TextIOBase .

encoding gives the name of the encoding that the stream will be decoded or encoded with. It defaults to locale.getpreferredencoding() .

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' 导致置换标记 (譬如 '?' ) to be inserted where there is malformed data. When writing, 'xmlcharrefreplace' (replace with the appropriate XML character reference) or 'backslashreplace' (replace with backslashed escape sequences) can be used. Any other error handling name that has been registered with codecs.register_error() is also valid.

newline controls how line endings are handled. It can be None , '' , '\n' , '\r' ,和 '\r\n' 。其工作如下:

  • On input, if newline is None , 通用换行符 mode is enabled. Lines in the input can end in '\n' , '\r' ,或 '\r\n' , and these are translated into '\n' before being returned to the caller. If it is '' , universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it 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.
  • On output, if newline is None ,任何 '\n' characters written are translated to the system default line separator, os.linesep 。若 newline is '' ,不发生翻译。若 newline is any of the other legal values, any '\n' characters written are translated to the given string.

line_buffering is True , flush() is implied when a call to write contains a newline character.

TextIOWrapper provides one attribute in addition to those of TextIOBase 及其父级:

line_buffering

行缓冲是否被启用。

class io. StringIO ( initial_value=u'' , newline=u'\n' )

An in-memory stream for unicode text. It inherits TextIOWrapper .

The initial value of the buffer (an empty unicode string by default) can be set by providing initial_value newline 自变量的工作像 TextIOWrapper . The default is to consider only \n characters as end of lines and to do no newline translation.

StringIO 提供此方法,除了那些来自 TextIOWrapper 及其父级:

getvalue ( )

返回 unicode containing the entire contents of the buffer at any time before the StringIO 对象的 close() 方法被调用。

用法范例:

import io
output = io.StringIO()
output.write(u'First line.\n')
output.write(u'Second line.\n')
# Retrieve file contents -- this will be
# u'First line.\nSecond line.\n'
contents = output.getvalue()
# Close object and discard memory buffer --
# .getvalue() will now raise an exception.
output.close()
						
class io. IncrementalNewlineDecoder

解码换行符的帮手编解码器,对于 通用换行符 模式。它继承 codecs.IncrementalDecoder .

15.2.6. Advanced topics

Here we will discuss several advanced topics pertaining to the concrete I/O implementations described above.

15.2.6.1. Performance

15.2.6.1.1. Binary I/O

By reading and writing only large chunks of data even when the user asks for a single byte, buffered I/O is designed to hide any inefficiency in calling and executing the operating system’s unbuffered I/O routines. The gain will vary very much depending on the OS and the kind of I/O which is performed (for example, on some contemporary 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 will offer you predictable performance regardless of the platform and the backing device. Therefore, it is most always preferable to use buffered I/O rather than unbuffered I/O.

15.2.6.1.2. Text I/O

Text I/O over a binary storage (such as a file) is significantly slower than binary I/O over the same storage, because it implies conversions from unicode to binary data using a character codec. This can become noticeable if you handle huge amounts of text data (for example very large log files). Also, TextIOWrapper.tell() and TextIOWrapper.seek() are both quite slow due to the reconstruction algorithm used.

StringIO ,不管怎样,是本机内存 unicode 容器,且显露速度类似 BytesIO .

15.2.6.2. Multi-threading

FileIO 对象在操作系统调用程度是线程安全的 (譬如 read(2) under Unix) they are wrapping are thread-safe too.

二进制缓冲对象 (实例化的 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 对象不是线程安全的。

15.2.6.3. Reentrancy

二进制缓冲对象 (实例化的 BufferedReader , BufferedWriter , BufferedRandom and BufferedRWPair ) are not reentrant. While reentrant calls will not happen in normal situations, they can arise if you are doing I/O in a signal handler. If it is attempted to enter a buffered object again while already being accessed from the same thread , then a 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 function print() 还。

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1