04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 10 ■ BATTERIES INCLUDED 219<br />

fileinput<br />

You learn a lot about reading from and writing to files in Chapter 11, but here is a sneak preview.<br />

The fileinput module enables you to easily iterate over all the lines in a series of text files. If<br />

you call your script like this (assuming a UNIX command line):<br />

$ python some_script.py file1.txt file2.txt file3.txt<br />

you will be able to iterate over the lines of file1.txt through file3.txt in turn. You can also<br />

iterate over lines supplied to standard input (sys.stdin, remember?), for example, in a UNIX<br />

pipe (using the standard UNIX command cat):<br />

$ cat file.txt | python some_script.py<br />

If you use fileinput, this way of calling your script (with cat in a UNIX pipe) works just as<br />

well as the previous one (supplying the file names as command-line arguments to your script).<br />

The most important functions of the fileinput module are described in Table 10-4.<br />

Table 10-4. Some Important Functions in the fileinput Module<br />

Function<br />

input([files[, inplace[, backup]])<br />

filename()<br />

lineno()<br />

filelineno()<br />

isfirstline()<br />

isstdin()<br />

nextfile()<br />

close()<br />

Description<br />

Facilitates iteration over lines in multiple input<br />

streams<br />

Returns name of current file<br />

Returns current (cumulative) line number<br />

Returns line number within current file<br />

Checks whether current line is first in file<br />

Checks whether last line was from sys.stdin<br />

Closes current file and moves to the next<br />

Closes the sequence<br />

The function fileinput.input is the most important of the functions. It returns an object<br />

that you can iterate over in a for loop. If you don’t want the default behavior (in which fileinput<br />

finds out which files to iterate over), you can supply one or more file names to this function<br />

(as a sequence). You can also set the inplace parameter to a true value (inplace=True) to enable<br />

in-place processing. For each line you access, you’ll have to print out a replacement, which will<br />

be put back into the current input file. The optional backup argument gives a file name extension to<br />

a backup file created from the original file when you do in-place processing.<br />

The function fileinput.filename returns the file name of the file you are currently in (that<br />

is, the file that contains the line you are currently processing).<br />

The function fileinput.lineno returns the number of the current line. This count is<br />

cumulative so that when you are finished with one file and begin processing the next, the line<br />

number is not reset but starts at one more than the last line number in the previous file.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!