10.2. fileinput — 迭代来自多个输入流的行

源代码: Lib/fileinput.py


此模块实现了一个辅助类和一些函数用来快速编写访问标准输入或文件列表的循环。 如果你只想要读写一个文件请参阅 open()

典型用法为:

import fileinput
for line in fileinput.input():
    process(line)

This iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is '-', it is also replaced by sys.stdin. To specify an alternative list of filenames, pass it as the first argument to input(). A single file name is also allowed.

All files are opened in text mode by default, but you can override this by specifying the mode parameter in the call to input() or FileInput(). If an I/O error occurs during opening or reading a file, IOError is raised.

如果 sys.stdin 被使用超过一次,则第二次之后的使用将不返回任何行,除非是被交互式的使用,或都是被显式地重置 (例如使用 sys.stdin.seek(0))。

空文件打开后将立即被关闭;它们在文件列表中会被注意到的唯一情况只有当最后打开的文件为空的时候。

反回的行不会对换行符做任何处理,这意味着文件中的最后一行可能不带换行符。

想要控制文件的打开方式,你可以通过将 openhook 形参传给 fileinput.input()FileInput() 来提供一个打开钩子。 此钩子必须为一个函数,它接受两个参数,filenamemode,并返回一个以相应模式打开的文件类对象。 此模块已经提供了两个有用的钩子。

以下函数是此模块的初始接口:

fileinput.input([files[, inplace[, backup[, bufsize[, mode[, openhook]]]]]])

创建一个 FileInput 类的实例。 该实例将被用作此模块中函数的全局状态,并且还将在迭代期间被返回使用。 此函数的形参将被继续传递给 FileInput 类的构造器。

在 2.5 版更改: Added the mode and openhook parameters.

在 2.7.12 版更改: The bufsize parameter is no longer used.

下列函数会使用 fileinput.input() 所创建的全局状态;如果没有活动的状态,则会引发 RuntimeError

fileinput.filename()

返回当前被读取的文件名。 在第一行被读取之前,返回 None

fileinput.fileno()

返回以整数表示的当前文件“文件描述符”。 当未打开文件时(处在第一行和文件之间),返回 -1

2.5 新版功能.

fileinput.lineno()

返回已被读取的累计行号。 在第一行被读取之前,返回 0。 在最后一个文件的最后一行被读取之后,返回该行的行号。

fileinput.filelineno()

返回当前文件中的行号。 在第一行被读取之前,返回 0。 在最后一个文件的最后一行被读取之后,返回此文件中该行的行号。

fileinput.isfirstline()

Returns true if the line just read is the first line of its file, otherwise returns false.

fileinput.isstdin()

Returns true if the last line was read from sys.stdin, otherwise returns false.

fileinput.nextfile()

关闭当前文件以使下次迭代将从下一个文件(如果存在)读取第一行;不是从该文件读取的行将不会被计入累计行数。 直到下一个文件的第一行被读取之后文件名才会改变。 在第一行被读取之前,此函数将不会生效;它不能被用来跳过第一个文件。 在最后一个文件的最后一行被读取之后,此函数将不再生效。

fileinput.close()

关闭序列。

此模块所提供的实现了序列行为的类同样也可用于子类化:

class fileinput.FileInput([files[, inplace[, backup[, bufsize[, mode[, openhook]]]]]])

Class FileInput is the implementation; its methods filename(), fileno(), lineno(), filelineno(), isfirstline(), isstdin(), nextfile() and close() correspond to the functions of the same name in the module. In addition it has a readline() method which returns the next input line, and a __getitem__() method which implements the sequence behavior. The sequence must be accessed in strictly sequential order; random access and readline() cannot be mixed.

通过 mode 你可以指定要传给 open() 的文件模式。 它必须为 'r', 'rU', 'U''rb' 中的一个。

openhook 如果给出则必须为一个函数,它接受两个参数 filenamemode,并相应地返回一个打开的文件类对象。 你不能同时使用 inplaceopenhook

在 2.5 版更改: Added the mode and openhook parameters.

在 2.7.12 版更改: The bufsize parameter is no longer used.

Optional in-place filtering: if the keyword argument inplace=1 is passed to fileinput.input() or to the FileInput constructor, the file is moved to a backup file and standard output is directed to the input file (if a file of the same name as the backup file already exists, it will be replaced silently). This makes it possible to write a filter that rewrites its input file in place. If the backup parameter is given (typically as backup='.<some extension>'), it specifies the extension for the backup file, and the backup file remains around; by default, the extension is '.bak' and it is deleted when the output file is closed. In-place filtering is disabled when standard input is read.

注解

The current implementation does not work for MS-DOS 8+3 filesystems.

此模块提供了以下两种打开文件钩子:

fileinput.hook_compressed(filename, mode)

使用 gzipbz2 模块透明地打开 gzip 和 bzip2 压缩的文件(通过扩展名 '.gz''.bz2' 来识别)。 如果文件扩展名不是 '.gz''.bz2',文件会以正常方式打开(即使用 open() 并且不带任何解压操作)。

使用示例: fi = fileinput.FileInput(openhook=fileinput.hook_compressed)

2.5 新版功能.

fileinput.hook_encoded(encoding)

Returns a hook which opens each file with io.open(), using the given encoding to read the file.

Usage example: fi = fileinput.FileInput(openhook=fileinput.hook_encoded("iso-8859-1"))

注解

With this hook, FileInput might return Unicode strings depending on the specified encoding.

2.5 新版功能.