Table of Contents generated with DocToc
- 18.1
bz2 - 18.2
filecmp - 18.3
fnmatch - 18.4
glob - 18.5
gzip - 18.6
shutil - 18.7
tarfile - 18.8
tempfile - 18.9
zipfile - 18.10
zlib - Navigation
The bz2 module is used to read and write data compressed according to the bzip2 compression algorithm.
BZ2File(filename [, mode [, buffering [, compresslevel]]])Opens a.bz2file,filename, and returns a file-like object.BZ2Compressor([compresslevel])Creates a compressor object that can be used to sequentially compress a sequence of data blocks. An instance, c, of BZ2Compressor has the following methods:c.compress(data)Feeds new string data to the compressor object,c. Returns a string of compressed data if possible.
BZ2Decompressor()Creates a decompressor object. An instance,d, ofBZ2Decompressorsupports just one method:d.decompress(data)Given a chunk of compressed data in the stringdata, this method returns uncompressed data.
compress(data [, compresslevel])Returns a compressed version of the data supplied in the stringdata.decompress(data)Returns a string containing the decompressed data in the stringdata.
The filecmp module provides the following functions, which can be used to compare files and directories:
cmp(file1, file2 [, shallow])Compares the filesfile1andfile2and returnsTrueif they’re equal,Falseif not.dircmp(dir1, dir2 [, ignore[, hide]])Creates a directory comparison object that can be used to perform various comparison operations on the directoriesdir1anddir2. A directory object,dhas following methods and attribues:d.report()Compares directoriesdir1anddir2and prints a report tosys.stdout.d.left_listLists the files and subdirectories indir1.d.common_filesLists the files that are common todir1anddir2.d.diff_filesLists the files with different contents indir1anddir2.
The fnmatch module provides support for matching filenames using UNIX shell-style wildcard characters. This module only performs filename matching, whereas the glob module can be used to actually obtain file listings.
fnmatch(filename, pattern) Returns True or False depending on whether filename matches pattern.
filter(names, pattern) Applies the fnmatch() function to all of the names in the sequence names and returns a list of all names that match pattern.
The glob module returns all filenames in a directory that match a pattern specified using the rules of the UNIX shell (as described in the fnmatch module).
glob(pattern) Returns a list of pathnames that match pattern.
The gzip module provides a class, GzipFile, that can be used to read and write files compatible with the GNU gzip program. GzipFile objects work like ordinary files except that data is automatically compressed or decompressed.
GzipFile([filename [, mode [, compresslevel [, fileobj]]]]) Opens a GzipFile.
The shutil module is used to perform high-level file operations such as copying, removing, and renaming. The functions in this module should only be used for proper files and directories. In particular, they do not work for special kinds of files on the file system such as named pipes, block devices, etc.
copy(src,dst)Copies the filesrcto the file or directorydst, retaining file permissions. src and dst are strings.move(src, dst)Moves a file or directorysrctodst.rmtree(path [, ignore_errors [, onerror]])Deletes an entire directory tree.
The tarfile module is used to manipulate tar archive files. Using this module, it is possible to read and write tar files, with or without compression.
open([name [, mode [, fileobj [, bufsize]]]]) Creates a new TarFile object with the pathname name.
A TarFile instance, t, returned by open() supports the following methods and attributes:
t.add(name [, arcname [, recursive]])Adds a new file to the tar archive.t.extract(member [, path])Extracts a member from the archive, saving it to the current directory.t.getmember(name)Looks up archive membernameand returns aTarInfoobject containing information about it.t.gettarinfo([name [, arcname [, fileobj]]])Returns aTarInfoobject corresponding to a file,name, on the file system or an open file object, fi`leobj. arcname is an alternative name for the object in the archive.t.list([verbose])Lists the contents of the archive tosys.stdout.t.next()A method used for iterating over the members of an archive.
The tempfile module is used to generate temporary filenames and files.
mkdtemp([suffix [,prefix [, dir]]])Creates a temporary directory accessible only by the owner of the calling process and returns its absolute pathname. -TemporaryFile([mode [, bufsize [, suffix [,prefix [, dir]]]]])Creates a temporary file usingmkstemp()and returns a file-like object that supports the same methods as an ordinary file object.NamedTemporaryFile([mode [, bufsize [, suffix [,prefix [, dir [, delete ]]]]]])Creates a temporary file just likeTemporaryFile()but makes sure the filename is visible on the file system.
The zipfile module is used to manipulate files encoded in the popular zip format (originally known as PKZIP, although now supported by a wide variety of programs).
ZipFile(filename [, mode [, compression [,allowZip64]]])Opens a zip file, filename, and returns a ZipFile instance.ZipInfo([filename [, date_time]])Manually creates a new ZipInfo instance, used to contain information about an archive member.
An instance, z, of ZipFile or PyZipFile supports the following methods and attributes:
z.extract(name [, path [, pwd ]])Extracts a file from the archive and places it in the current working directory.z.open(name [, mode [, pwd]])Opens an archive member named name and returns a file-like object for reading the contents.z.read(name [,pwd])Reads archive contents for member name and returns the data as a string.z.write(filename[, arcname[, compress_type]])Writes filename to the archive with the archive name arcname.
The zlib module supports data compression by providing access to the zlib library.
compress(string [, level])Compresses the data in string, where level is an integer from 1 to 9 controlling the level of compression.decompress(string [, wbits [, buffsize]])Decompresses the data in string. wbits controls the size of the window buffer, and buffsize is the initial size of the output buffer. Raises error if an error occurs.