"fileinput" — 複数の入力ストリームをまたいだ行の繰り返し処理をサポートする
**************************************************************************

**ソースコード:** Lib/fileinput.py

======================================================================

このモジュールは標準入力やファイルの並びにまたがるループを素早く書くた
めのヘルパークラスと関数を提供しています。単一のファイルを読み書きした
いだけなら、 "open()" を参照してください。

典型的な使い方は以下の通りです:

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

このプログラムは "sys.argv[1:]" に含まれる全てのファイルをまたいで繰り
返します。もし該当するものがなければ、 "sys.stdin" がデフォルトとして
扱われます。ファイル名として "'-'" が与えられた場合も、 "sys.stdin" に
置き換えられます。別のファイル名リストを使いたい時には、 "input()" の
最初の引数にリストを与えます。単一ファイル名の文字列も受け付けます。

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" が2回以上使われた場合は、2回目以降は行を返しません。ただし
インタラクティブに利用している時や明示的にリセット
("sys.stdin.seek(0)" を使う) を行った場合はその限りではありません。

空のファイルは開いた後すぐ閉じられます。空のファイルはファイル名リスト
の最後にある場合にしか外部に影響を与えません。

ファイルの各行は、各種改行文字まで含めて返されます。ファイルの最後が改
行文字で終っていない場合には、改行文字で終わらない行が返されます。

ファイルのオープン方法を制御するためのオープン時フックは、
"fileinput.input()" あるいは "FileInput()" の *openhook* パラメータで
設定します。このフックは、ふたつの引数 *filename* と *mode* をとる関数
でなければなりません。そしてその関数の返り値はオープンしたファイルオブ
ジェクトとなります。このモジュールには、便利なフックが既に用意されてい
ます。

以下の関数がこのモジュールの基本的なインタフェースです:

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()

   最後に読み込まれた行の、累積した行番号を返します。1行目が読み込まれ
   る前は "0" を返します。最後のファイルの最終行が読み込まれた後には、
   その行の行番号を返します。

fileinput.filelineno()

   現在のファイル中での行番号を返します。1行目が読み込まれる前は "0"
   を返します。最後のファイルの最終行が読み込まれた後には、その行のフ
   ァイル中での行番号を返します。

fileinput.isfirstline()

   最後に読み込まれた行がファイルの 1 行目なら True、そうでなければ
   False を返します。

fileinput.isstdin()

   最後に読み込まれた行が "sys.stdin" から読まれていれば True、そうで
   なければ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* を指定する場合は、ふたつの引数 *filename* と *mode* をと
   る関数でなければなりません。この関数の返り値は、オープンしたファイ
   ルオブジェクトとなります。*inplace* と *openhook* を同時に使うこと
   はできません。

   バージョン 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)

   gzip や bzip2 で圧縮された (拡張子が "'.gz'" や "'.bz2'" の) ファイ
   ルを、 "gzip" モジュールや "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 で追加.
