Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:5.10.1 ConfigParser ObjectsUp:5. Miscellaneous ServicesNext:5.12 xreadlines

5.11fileinput -- Iterate over lines from multiple input streams

This module implements a helper class and functions to quickly write aloop over standard input or a list of files.

The typical use is:

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

This iterates over the lines of all files listed insys.argv[1:], defaulting tosys.stdin if the list isempty. If a filename is'-', it is also replaced bysys.stdin. To specify an alternative list of filenames, passit as the first argument toinput(). A single file name isalso allowed.

All files are opened in text mode. If an I/O error occurs duringopening or reading a file,IOError is raised.

Ifsys.stdin is used more than once, the second and further usewill return no lines, except perhaps for interactive use, or if it hasbeen explicitly reset (e.g. usingsys.stdin.seek(0)).

Empty files are opened and immediately closed; the only time theirpresence in the list of filenames is noticeable at all is when thelast file opened is empty.

It is possible that the last line of a file does not end in a newlinecharacter; lines are returned including the trailing newline when itis present.

The following function is the primary interface of this module:

input([files[, inplace[, backup]]])
Create an instance of theFileInput class. The instance will be used as global state for the functions of this module, and is also returned to use during iteration. The parameters to this function will be passed along to the constructor of theFileInput class.

The following functions use the global state created byinput(); if there is no active state,RuntimeError is raised.

filename()
Return the name of the file currently being read. Before the first line has been read, returnsNone.

lineno()
Return the cumulative line number of the line that has just been read. Before the first line has been read, returns0. After the last line of the last file has been read, returns the line number of that line.

filelineno()
Return the line number in the current file. Before the first line has been read, returns0. After the last line of the last file has been read, returns the line number of that line within the file.

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

isstdin()
Returns true if the last line was read fromsys.stdin, otherwise returns false.

nextfile()
Close the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count towards the cumulative line count. The filename is not changed until after the first line of the next file has been read. Before the first line has been read, this function has no effect; it cannot be used to skip the first file. After the last line of the last file has been read, this function has no effect.

close()
Close the sequence.

The class which implements the sequence behavior provided by themodule is available for subclassing as well:

classFileInput([files[, inplace[, backup]]])
ClassFileInput is the implementation; its methodsfilename(),lineno(),fileline(),isfirstline(),isstdin(),nextfile() andclose() correspond to the functions of the same name in the module. In addition it has areadline() 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 andreadline() cannot be mixed.

Optional in-place filtering: if the keyword argumentinplace=1 is passed toinput() or to theFileInput constructor, the file is moved to a backup file andstandard output is directed to the input file (if a file of the samename as the backup file already exists, it will be replaced silently).This makes it possible to write a filter that rewrites its input filein place. If the keyword argumentbackup='.<someextension>' is also given, it specifies the extension for the backupfile, and the backup file remains around; by default, the extension is'.bak' and it is deleted when the output file is closed. In-placefiltering is disabled when standard input is read.

Caveat: The current implementation does not work for MS-DOS8+3 filesystems.


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:5.10.1 ConfigParser ObjectsUp:5. Miscellaneous ServicesNext:5.12 xreadlines
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp