Module shutil
[hide private]
[frames] | no frames]

Module shutil

source code

Utility functions for copying and archiving files and directory trees.

XXX The functions here don't copy the resource fork or other metadata on Mac.

Classes [hide private]
  Error
  ExecError
Raised when a command could not be executed
  SpecialFileError
Raised when trying to do a kind of operation (e.g.
Functions [hide private]
 
_basename(path) source code
 
_call_external_zip(base_dir, zip_filename, verbose=False, dry_run=False) source code
 
_destinsrc(src, dst) source code
 
_get_gid(name)
Returns a gid, given a group name.
source code
 
_get_uid(name)
Returns an uid, given a user name.
source code
 
_make_tarball(base_name, base_dir, compress='gzip', verbose=0, dry_run=0, owner=None, group=None, logger=None)
Create a (possibly compressed) tar file from all the files under 'base_dir'.
source code
 
_make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None)
Create a zip file from all the files under 'base_dir'.
source code
 
_samefile(src, dst) source code
 
copy(src, dst)
Copy data and mode bits ("cp src dst").
source code
 
copy2(src, dst)
Copy data and all stat info ("cp -p src dst").
source code
 
copyfile(src, dst)
Copy data from src to dst
source code
 
copyfileobj(fsrc, fdst, length=16384)
copy data from file-like object fsrc to file-like object fdst
source code
 
copymode(src, dst)
Copy mode bits from src to dst
source code
 
copystat(src, dst)
Copy all stat info (mode bits, atime, mtime, flags) from src to dst
source code
 
copytree(src, dst, symlinks=False, ignore=None)
Recursively copy a directory tree using copy2().
source code
 
get_archive_formats()
Returns a list of supported formats for archiving and unarchiving.
source code
 
ignore_patterns(*patterns)
Function that can be used as copytree() ignore parameter.
source code
 
make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, dry_run=0, owner=None, group=None, logger=None)
Create an archive file (eg.
source code
 
move(src, dst)
Recursively move a file or directory to another location.
source code
 
register_archive_format(name, function, extra_args=None, description='')
Registers an archive format.
source code
 
rmtree(path, ignore_errors=False, onerror=None)
Recursively delete a directory tree.
source code
 
unregister_archive_format(name) source code
Variables [hide private]
  WindowsError = None
  _ARCHIVE_FORMATS = {'bztar': (<function _make_tarball at 0x324...
  __package__ = None
Function Details [hide private]

_make_tarball(base_name, base_dir, compress='gzip', verbose=0, dry_run=0, owner=None, group=None, logger=None)

source code 

Create a (possibly compressed) tar file from all the files under 'base_dir'.

'compress' must be "gzip" (the default), "bzip2", or None.

'owner' and 'group' can be used to define an owner and a group for the archive that is being built. If not provided, the current owner and group will be used.

The output tar file will be named 'base_name' + ".tar", possibly plus the appropriate compression extension (".gz", or ".bz2").

Returns the output filename.

_make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None)

source code 

Create a zip file from all the files under 'base_dir'.

The output zip file will be named 'base_name' + ".zip". Uses either the "zipfile" Python module (if available) or the InfoZIP "zip" utility (if installed and found on the default search path). If neither tool is available, raises ExecError. Returns the name of the output zip file.

copy(src, dst)

source code 

Copy data and mode bits ("cp src dst").

The destination may be a directory.

copy2(src, dst)

source code 

Copy data and all stat info ("cp -p src dst").

The destination may be a directory.

copytree(src, dst, symlinks=False, ignore=None)

source code 
Recursively copy a directory tree using copy2().

The destination directory must not already exist.
If exception(s) occur, an Error is raised with a list of reasons.

If the optional symlinks flag is true, symbolic links in the
source tree result in symbolic links in the destination tree; if
it is false, the contents of the files pointed to by symbolic
links are copied.

The optional ignore argument is a callable. If given, it
is called with the `src` parameter, which is the directory
being visited by copytree(), and `names` which is the list of
`src` contents, as returned by os.listdir():

    callable(src, names) -> ignored_names

Since copytree() is called recursively, the callable will be
called once for each directory that is copied. It returns a
list of names relative to the `src` directory that should
not be copied.

XXX Consider this example code rather than the ultimate tool.

get_archive_formats()

source code 

Returns a list of supported formats for archiving and unarchiving.

Each element of the returned sequence is a tuple (name, description)

ignore_patterns(*patterns)

source code 

Function that can be used as copytree() ignore parameter.

Patterns is a sequence of glob-style patterns that are used to exclude files

make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, dry_run=0, owner=None, group=None, logger=None)

source code 

Create an archive file (eg. zip or tar).

'base_name' is the name of the file to create, minus any format-specific extension; 'format' is the archive format: one of "zip", "tar", "bztar" or "gztar".

'root_dir' is a directory that will be the root directory of the archive; ie. we typically chdir into 'root_dir' before creating the archive. 'base_dir' is the directory where we start archiving from; ie. 'base_dir' will be the common prefix of all files and directories in the archive. 'root_dir' and 'base_dir' both default to the current directory. Returns the name of the archive file.

'owner' and 'group' are used when creating a tar archive. By default, uses the current owner and group.

move(src, dst)

source code 

Recursively move a file or directory to another location. This is similar to the Unix "mv" command.

If the destination is a directory or a symlink to a directory, the source is moved inside the directory. The destination path must not already exist.

If the destination already exists but is not a directory, it may be overwritten depending on os.rename() semantics.

If the destination is on our current filesystem, then rename() is used. Otherwise, src is copied to the destination and then removed. A lot more could be done here... A look at a mv.c shows a lot of the issues this implementation glosses over.

register_archive_format(name, function, extra_args=None, description='')

source code 

Registers an archive format.

name is the name of the format. function is the callable that will be used to create archives. If provided, extra_args is a sequence of (name, value) tuples that will be passed as arguments to the callable. description can be provided to describe the format, and will be returned by the get_archive_formats() function.

rmtree(path, ignore_errors=False, onerror=None)

source code 

Recursively delete a directory tree.

If ignore_errors is set, errors are ignored; otherwise, if onerror is set, it is called to handle the error with arguments (func, path, exc_info) where func is os.listdir, os.remove, or os.rmdir; path is the argument to that function that caused it to fail; and exc_info is a tuple returned by sys.exc_info(). If ignore_errors is false and onerror is None, an exception is raised.


Variables Details [hide private]

_ARCHIVE_FORMATS

Value:
{'bztar': (<function _make_tarball at 0x3248230>,
           [('compress', 'bzip2')],
           'bzip2\'ed tar-file'),
 'gztar': (<function _make_tarball at 0x3248230>,
           [('compress', 'gzip')],
           'gzip\'ed tar-file'),
 'tar': (<function _make_tarball at 0x3248230>,
         [('compress', None)],
...