Auxiliary Modules for File I/O
File objects supply all the minimal indispensable functionality needed for file I/O. Some auxiliary Python library modules, however, offer convenient supplementary functionality, making I/O even easier and handier in several important cases.
The fileinput Module
The fileinput
module lets you loop over all the lines in a list of text files. Performance is good, comparable to the performance of direct iteration on each file, since fileinput
uses buffering to minimize I/O. You can therefore use module fileinput
for line-oriented file input whenever you find the module’s rich functionality convenient, with no worries about performance. The input
function is the key function of module fileinput
, and the module also provides a FileInput
class whose methods support the same functionality as the module’s functions.
close |
Closes the whole sequence so that iteration stops and no file remains open. |
FileInput |
Creates and returns an instance |
filelineno |
Returns the number of lines read so far from the file now being read. ... |
Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.