9.10. Related Modules

There are plenty of other modules related to files and input/output, all of which work on most of the major platforms. Table9.7 lists some of the file-related modules.

Table 9.7. Related File Modules
Module(s)Contents
fileinputiterates over lines of multiple input text files
getoptprovides command-line argument parsing/manipulation
glob/fnmatchprovides Unix-style wildcard character matching
gzip/zlib/zipfile[a]allows file access to include automatic de/compression
shutiloffers high-level file access functionality
c/StringIOimplements file-like interface on top of string objects
tempfilegenerates temporary file names or files

[a] new in Python 1.6

The fileinput module iterates over a set of input files and reads their contents one line at a time, allowing you to iterate over each line, much like the way the Perl ( < > ) operator works without any provided arguments. File names that are not explicitly given will be assumed to be provided from the command-line.

The glob and fnmatch modules allow for file name pattern-matching in the good old fashioned Unix shell-style, for example, using the asterisk ( * ) wildcard character for all string matches and the ( ? ) for matching single characters.

The gzip and zlib modules provide direct file access to the zlib compression library. The gzip module, written on top of the zlib module, allows for standard file access, but provides for automatic gzip-compatible compression and decompression. Note that if you are compiling your Python interpreter, you have to enable the zlib module to be built (by editing the Modules/Setup file). It is not turned on by default. The new zipfile module, also requiring the zlib module, allows the programmer to create, modify, and read zip archive files.

The shutil module furnishes high-level file access, performing such functions as copying files, copying file permissions, and recursive directory tree copying, to name a few.

The tempfile module can be used to generate temporary file names and files.

In our earlier chapter on strings, we described the StringIO module (and its C-compiled companion cStringIO), and how it overlays a file interface on top of string objects. This interface includes all of the standard methods available to regular file objects.

The modules we mentioned in the Persistent Storage section above (Section 9.9) include examples of a hybrid file- and dictionary-like object.

Some other Python modules which generate file-like objects include network and file socket objects (socket module), the popen*() file objects that connect your application to other running processes (os and popen2 modules), the fdopen() file object used in low-level file access (os module), and opening a network connection to an Internet web server via its Uniform Resource Locator (URL) address (urllib module). Please be aware that not all standard file methods may be implemented for these objects. Likewise, they may provide functionality in addition to what is available for regular files.

Refer to the documentation for more details on these file access-related modules.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset