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