Section 9.10. Related Modules


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. Table 9.7 lists some of the filerelated modules.

Table 9.7. Related File Modules

Module(s)

Contents

base64

Encoding/decoding of binary strings to/from text strings

binascii

Encoding/decoding of binary and ASCII-encoded binary strings

bz2[a]

Allows access to BZ2 compressed files

csv[a]

Allows access to comma-separated value files

filecmp[b].

Compares directories and files

fileinput

Iterates over lines of multiple input text files

getopt/optparse[a]

Provides command-line argument parsing/manipulation

glob/fnmatch

Provides Unix-style wildcard character matching

gzip/zlib

Reads and writes GNU zip (gzip) files (needs zlib module for compression)

shutil

Offers high-level file access functionality

c/StringIO

Implements file-like interface on top of string objects

tarfile[a]

Reads and writes TAR archive files, even compressed ones

tempfile

Generates temporary file names or files

uu

uuencode and uudecode files

zipfile[c]

Tools and utilities to read and write ZIP archive files


[a] New in Python 2.3.

[b] New in Python 2.0.

[c] 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.

Core Tip: Tilde ( ~ ) expansion via os.path.expanduser()

Although the glob and fnmatch allow for Unix-style pattern-matching, they do not enable the expansion of the tilde (home directory) character, ~ . This is handled by the os.path.expanduser() function. You pass in a path containing a tilde, and it returns the equivalent absolute file path. Here are two examples, in a Unix-based environment and in Win32:

>>> os.path.expanduser('~;/py') '/home/wesley/py' >>> os.path.expanduser('~;/py') 'C:\\Documents and Settings\\wesley/py'


In addition, Unix-flavored systems also support the " ~user " notation indicating the home directory for a specific user. Also, note that the Win32 version does not change forward slashes to the DOS backslashes in a directory path.


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. bz2 is like gzip but for bzipped files.

The zipfile module introduced in 1.6 allows the programmer to create, modify, and read zip archive files. (The tarfile module serves as an equivalent for TAR archive files.) In 2.3, Python was given the ability to import modules archived in zip files as well. See Section 12.5.7 for more information.

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 filenames 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 that 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. In addition, you can find out more about file()/open(), files, file objects, and UNS at:

http://docs.python.org/lib/built-in-funcs.html

http://docs.python.org/lib/bltin-file-objects.html

http://www.python.org/doc/2.3/whatsnew/node7.html

http://www.python.org/doc/peps/pep-0278/



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

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