The fileinput Module

The fileinput module allows you to loop over the contents of one or more text files, as shown in Example 2-1.

Example 2-1. Using the fileinput Module to Loop Over a Text File

File: fileinput-example-1.py

import fileinput
import sys

for line in fileinput.input("samples/sample.txt"):
 sys.stdout.write("-> ")
 sys.stdout.write(line)

-> We will perhaps eventually be writing only small
-> modules which are identified by name as they are
-> used to build larger ones, so that devices like
-> indentation, rather than delimiters, might become
-> feasible for expressing local structure in the
-> source language.
-> -- Donald E. Knuth, December 1974

The fileinput module also allows you to get metainformation about the current line. This includes isfirstline, filename, and lineno, as Example 2-2 shows.

Example 2-2. Using the fileinput Module to Process Multiple Files

File: fileinput-example-2.py

import fileinput
import glob
import string, sys

for line in fileinput.input(glob.glob("samples/*.txt")):
 if fileinput.isfirstline(): # first in a file?
 sys.stderr.write("-- reading %s --
" % fileinput.filename())
 sys.stdout.write(str(fileinput.lineno()) + " " + string.upper(line))

-- reading samplessample.txt --
1 WE WILL PERHAPS EVENTUALLY BE WRITING ONLY SMALL
2 MODULES WHICH ARE IDENTIFIED BY NAME AS THEY ARE
3 USED TO BUILD LARGER ONES, SO THAT DEVICES LIKE
4 INDENTATION, RATHER THAN DELIMITERS, MIGHT BECOME
5 FEASIBLE FOR EXPRESSING LOCAL STRUCTURE IN THE
6 SOURCE LANGUAGE.
7 -- DONALD E. KNUTH, DECEMBER 1974

Processing text files in place is also easy. Just call the input function with the inplace keyword argument set to 1, and the module takes care of the rest. Example 2-3 demonstrates this.

Example 2-3. Using the fileinput Module to Convert CRLF to LF

File: fileinput-example-3.py

import fileinput, sys

for line in fileinput.input(inplace=1):
 # convert Windows/DOS text files to Unix files
 if line[-2:] == "
":
 line = line[:-2] + "
"
 sys.stdout.write(line)

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols

Internationalization

Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules

show all menu





Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net