fileinput --- 迭代來自多個輸入流的行?

源代碼: Lib/fileinput.py


此模塊實現(xiàn)了一個輔助類和一些函數(shù)用來快速編寫訪問標準輸入或文件列表的循環(huán)。 如果你只想要讀寫一個文件請參閱 open()。

典型用法為:

import fileinput
for line in fileinput.input(encoding="utf-8"):
    process(line)

此程序會迭代 sys.argv[1:] 中列出的所有文件內(nèi)的行,如果列表為空則會使用 sys.stdin。 如果有一個文件名為 '-',它也會被替換為 sys.stdin 并且可選參數(shù) modeopenhook 會被忽略。 要指定替代文件列表,請將其作為第一個參數(shù)傳給 input()。 也允許使用單個文件。

所有文件都默認以文本模式打開,但你可以通過在調(diào)用 input()FileInput 時指定 mode 形參來重載此行為。 如果在打開或讀取文件時發(fā)生了 I/O 錯誤,將會引發(fā) OSError。

在 3.3 版更改: 原來會引發(fā) IOError;現(xiàn)在它是 OSError 的別名。

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

空文件打開后將立即被關(guān)閉;它們在文件列表中會被注意到的唯一情況只有當最后打開的文件為空的時候。

反回的行不會對換行符做任何處理,這意味著文件中的最后一行可能不帶換行符。

You can control how files are opened by providing an opening hook via the openhook parameter to fileinput.input() or FileInput(). The hook must be a function that takes two arguments, filename and mode, and returns an accordingly opened file-like object. If encoding and/or errors are specified, they will be passed to the hook as additional keyword arguments. This module provides a hook_compressed() to support compressed files.

以下函數(shù)是此模塊的初始接口:

fileinput.input(files=None, inplace=False, backup='', *, mode='r', openhook=None, encoding=None, errors=None)?

創(chuàng)建一個 FileInput 類的實例。 該實例將被用作此模塊中函數(shù)的全局狀態(tài),并且還將在迭代期間被返回使用。 此函數(shù)的形參將被繼續(xù)傳遞給 FileInput 類的構(gòu)造器。

FileInput 實例可以在 with 語句中被用作上下文管理器。 在這個例子中,inputwith 語句結(jié)束后將會被關(guān)閉,即使發(fā)生了異常也是如此:

with fileinput.input(files=('spam.txt', 'eggs.txt'), encoding="utf-8") as f:
    for line in f:
        process(line)

在 3.2 版更改: 可以被用作上下文管理器。

在 3.8 版更改: 關(guān)鍵字形參 modeopenhook 現(xiàn)在是僅限關(guān)鍵字形參。

在 3.10 版更改: 增加了僅限關(guān)鍵字形參 encodingerrors。

下列函數(shù)會使用 fileinput.input() 所創(chuàng)建的全局狀態(tài);如果沒有活動的狀態(tài),則會引發(fā) RuntimeError。

fileinput.filename()?

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

fileinput.fileno()?

返回以整數(shù)表示的當前文件“文件描述符”。 當未打開文件時(處在第一行和文件之間),返回 -1

fileinput.lineno()?

返回已被讀取的累計行號。 在第一行被讀取之前,返回 0。 在最后一個文件的最后一行被讀取之后,返回該行的行號。

fileinput.filelineno()?

返回當前文件中的行號。 在第一行被讀取之前,返回 0。 在最后一個文件的最后一行被讀取之后,返回此文件中該行的行號。

fileinput.isfirstline()?

如果剛讀取的行是其所在文件的第一行則返回 True,否則返回 False。

fileinput.isstdin()?

如果最后讀取的行來自 sys.stdin 則返回 True,否則返回 False

fileinput.nextfile()?

關(guān)閉當前文件以使下次迭代將從下一個文件(如果存在)讀取第一行;不是從該文件讀取的行將不會被計入累計行數(shù)。 直到下一個文件的第一行被讀取之后文件名才會改變。 在第一行被讀取之前,此函數(shù)將不會生效;它不能被用來跳過第一個文件。 在最后一個文件的最后一行被讀取之后,此函數(shù)將不再生效。

fileinput.close()?

關(guān)閉序列。

此模塊所提供的實現(xiàn)了序列行為的類同樣也可用于子類化:

class fileinput.FileInput(files=None, inplace=False, backup='', *, mode='r', openhook=None, encoding=None, errors=None)?

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 is iterable and has a readline() method which returns the next input line. The sequence must be accessed in strictly sequential order; random access and readline() cannot be mixed.

With mode you can specify which file mode will be passed to open(). It must be one of 'r' and 'rb'.

openhook 如果給出則必須為一個函數(shù),它接受兩個參數(shù) filenamemode,并相應(yīng)地返回一個打開的文件類對象。 你不能同時使用 inplaceopenhook。

你可以指定 encodingerrors 來將其傳給 open()openhook。

FileInput 實例可以在 with 語句中被用作上下文管理器。 在這個例子中,inputwith 語句結(jié)束后將會被關(guān)閉,即使發(fā)生了異常也是如此:

with FileInput(files=('spam.txt', 'eggs.txt')) as input:
    process(input)

在 3.2 版更改: 可以被用作上下文管理器。

在 3.8 版更改: 關(guān)鍵字形參 modeopenhook 現(xiàn)在是僅限關(guān)鍵字形參。

在 3.10 版更改: 增加了僅限關(guān)鍵字形參 encodingerrors。

在 3.11 版更改: The 'rU' and 'U' modes and the __getitem__() method have been removed.

可選的原地過濾: 如果傳遞了關(guān)鍵字參數(shù) inplace=Truefileinput.input()FileInput 構(gòu)造器,則文件會被移至備份文件并將標準輸出定向到輸入文件(如果已存在與備份文件同名的文件,它將被靜默地替換)。 這使得編寫一個能夠原地重寫其輸入文件的過濾器成為可能。 如果給出了 backup 形參 (通常形式為 backup='.<some extension>'),它將指定備份文件的擴展名,并且備份文件會被保留;默認情況下擴展名為 '.bak' 并且它會在輸出文件關(guān)閉時被刪除。 在讀取標準輸入時原地過濾會被禁用。

此模塊提供了以下兩種打開文件鉤子:

fileinput.hook_compressed(filename, mode, *, encoding=None, errors=None)?

使用 gzipbz2 模塊透明地打開 gzip 和 bzip2 壓縮的文件(通過擴展名 '.gz''.bz2' 來識別)。 如果文件擴展名不是 '.gz''.bz2',文件會以正常方式打開(即使用 open() 并且不帶任何解壓操作)。

encodingerrors 值會被傳給 io.TextIOWrapper 用于壓縮文件以及打開普通文件。

用法示例: fi = fileinput.FileInput(openhook=fileinput.hook_compressed, encoding="utf-8")

在 3.10 版更改: 增加了僅限關(guān)鍵字形參 encodingerrors。

fileinput.hook_encoded(encoding, errors=None)?

返回一個通過 open() 打開每個文件的鉤子,使用給定的 encodingerrors 來讀取文件。

使用示例: fi = fileinput.FileInput(openhook=fileinput.hook_encoded("utf-8", "surrogateescape"))

在 3.6 版更改: 添加了可選的 errors 形參。

3.10 版后已移除: This function is deprecated since fileinput.input() and FileInput now have encoding and errors parameters.