Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 1 | \section{\module{shutil} --- |
Fred Drake | dbd72a4 | 1999-02-01 21:27:59 +0000 | [diff] [blame] | 2 | High-level file operations} |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 3 | |
| 4 | \declaremodule{standard}{shutil} |
Fred Drake | dbd72a4 | 1999-02-01 21:27:59 +0000 | [diff] [blame] | 5 | \modulesynopsis{High-level file operations, including copying.} |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 6 | \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
| 7 | % partly based on the docstrings |
| 8 | |
| 9 | |
| 10 | The \module{shutil} module offers a number of high-level operations on |
| 11 | files and collections of files. In particular, functions are provided |
| 12 | which support file copying and removal. |
Fred Drake | 94c4a79 | 1998-12-28 21:58:57 +0000 | [diff] [blame] | 13 | \index{file!copying} |
| 14 | \index{copying files} |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 15 | |
| 16 | \strong{Caveat:} On MacOS, the resource fork and other metadata are |
| 17 | not used. For file copies, this means that resources will be lost and |
| 18 | file type and creator codes will not be correct. |
| 19 | |
| 20 | |
| 21 | \begin{funcdesc}{copyfile}{src, dst} |
| 22 | Copy the contents of \var{src} to \var{dst}. If \var{dst} exists, |
| 23 | it will be replaced, otherwise it will be created. |
| 24 | \end{funcdesc} |
| 25 | |
Fred Drake | 578a3f9 | 2000-07-31 15:45:46 +0000 | [diff] [blame] | 26 | \begin{funcdesc}{copyfileobj}{fsrc, fdst\optional{, length}} |
| 27 | Copy the contents of the file-like object \var{fsrc} to the |
| 28 | file-like object \var{fdst}. The integer \var{length}, if given, |
| 29 | is the buffer size. In particular, a negative \var{length} value |
| 30 | means to copy the data without looping over the source data in |
| 31 | chunks; by default the data is read in chunks to avoid uncontrolled |
| 32 | memory consumption. |
| 33 | \end{funcdesc} |
| 34 | |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 35 | \begin{funcdesc}{copymode}{src, dst} |
| 36 | Copy the permission bits from \var{src} to \var{dst}. The file |
| 37 | contents, owner, and group are unaffected. |
| 38 | \end{funcdesc} |
| 39 | |
| 40 | \begin{funcdesc}{copystat}{src, dst} |
| 41 | Copy the permission bits, last access time, and last modification |
| 42 | time from \var{src} to \var{dst}. The file contents, owner, and |
| 43 | group are unaffected. |
| 44 | \end{funcdesc} |
| 45 | |
| 46 | \begin{funcdesc}{copy}{src, dst} |
| 47 | Copy the file \var{src} to the file or directory \var{dst}. If |
| 48 | \var{dst} is a directory, a file with the same basename as \var{src} |
| 49 | is created (or overwritten) in the directory specified. Permission |
| 50 | bits are copied. |
| 51 | \end{funcdesc} |
| 52 | |
| 53 | \begin{funcdesc}{copy2}{src, dst} |
| 54 | Similar to \function{copy()}, but last access time and last |
| 55 | modification time are copied as well. This is similar to the |
Fred Drake | d290c10 | 1999-11-09 18:03:00 +0000 | [diff] [blame] | 56 | \UNIX{} command \program{cp} \programopt{-p}. |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 57 | \end{funcdesc} |
| 58 | |
| 59 | \begin{funcdesc}{copytree}{src, dst\optional{, symlinks}} |
| 60 | Recursively copy an entire directory tree rooted at \var{src}. The |
| 61 | destination directory, named by \var{dst}, must not already exist; |
| 62 | it will be created. Individual files are copied using |
| 63 | \function{copy2()}. If \var{symlinks} is true, symbolic links in |
| 64 | the source tree are represented as symbolic links in the new tree; |
| 65 | if false or omitted, the contents of the linked files are copied to |
| 66 | the new tree. Errors are reported to standard output. |
| 67 | |
| 68 | The source code for this should be considered an example rather than |
| 69 | a tool. |
| 70 | \end{funcdesc} |
| 71 | |
| 72 | \begin{funcdesc}{rmtree}{path\optional{, ignore_errors\optional{, onerror}}} |
Fred Drake | 94c4a79 | 1998-12-28 21:58:57 +0000 | [diff] [blame] | 73 | \index{directory!deleting} |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 74 | Delete an entire directory tree. If \var{ignore_errors} is true, |
| 75 | errors will be ignored; if false or omitted, errors are handled by |
| 76 | calling a handler specified by \var{onerror} or raise an exception. |
| 77 | |
| 78 | If \var{onerror} is provided, it must be a callable that accepts |
| 79 | three parameters: \var{function}, \var{path}, and \var{excinfo}. |
| 80 | The first parameter, \var{function}, is the function which raised |
| 81 | the exception; it will be \function{os.remove()} or |
| 82 | \function{os.rmdir()}. The second parameter, \var{path}, will be |
| 83 | the path name passed to \var{function}. The third parameter, |
| 84 | \var{excinfo}, will be the exception information return by |
| 85 | \function{sys.exc_info()}. Exceptions raised by \var{onerror} will |
| 86 | not be caught. |
| 87 | \end{funcdesc} |
| 88 | |
| 89 | |
| 90 | \subsection{Example \label{shutil-example}} |
| 91 | |
| 92 | This example is the implementation of the \function{copytree()} |
Fred Drake | 11bc8cf | 1999-04-21 17:08:51 +0000 | [diff] [blame] | 93 | function, described above, with the docstring omitted. It |
| 94 | demonstrates many of the other functions provided by this module. |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 95 | |
| 96 | \begin{verbatim} |
| 97 | def copytree(src, dst, symlinks=0): |
| 98 | names = os.listdir(src) |
| 99 | os.mkdir(dst) |
| 100 | for name in names: |
| 101 | srcname = os.path.join(src, name) |
| 102 | dstname = os.path.join(dst, name) |
| 103 | try: |
| 104 | if symlinks and os.path.islink(srcname): |
| 105 | linkto = os.readlink(srcname) |
| 106 | os.symlink(linkto, dstname) |
| 107 | elif os.path.isdir(srcname): |
| 108 | copytree(srcname, dstname) |
| 109 | else: |
| 110 | copy2(srcname, dstname) |
| 111 | # XXX What about devices, sockets etc.? |
| 112 | except (IOError, os.error), why: |
| 113 | print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why)) |
| 114 | \end{verbatim} |