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} |
Fred Drake | 043d5e5 | 2001-03-02 16:46:42 +0000 | [diff] [blame] | 22 | Copy the contents of the file named \var{src} to a file named |
Andrew M. Kuchling | 99872c1 | 2004-05-05 17:21:51 +0000 | [diff] [blame] | 23 | \var{dst}. The destination location must be writable; otherwise, |
| 24 | an \exception{IOError} exception will be raised. |
| 25 | If \var{dst} already exists, it will be replaced. |
| 26 | Special files such as character or block devices |
| 27 | and pipes cannot be copied with this function. \var{src} and |
Fred Drake | 757f780 | 2001-09-04 18:26:27 +0000 | [diff] [blame] | 28 | \var{dst} are path names given as strings. |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 29 | \end{funcdesc} |
| 30 | |
Fred Drake | 578a3f9 | 2000-07-31 15:45:46 +0000 | [diff] [blame] | 31 | \begin{funcdesc}{copyfileobj}{fsrc, fdst\optional{, length}} |
| 32 | Copy the contents of the file-like object \var{fsrc} to the |
| 33 | file-like object \var{fdst}. The integer \var{length}, if given, |
| 34 | is the buffer size. In particular, a negative \var{length} value |
| 35 | means to copy the data without looping over the source data in |
| 36 | chunks; by default the data is read in chunks to avoid uncontrolled |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 37 | memory consumption. Note that if the current file position of the |
| 38 | \var{fsrc} object is not 0, only the contents from the current file |
| 39 | position to the end of the file will be copied. |
Fred Drake | 578a3f9 | 2000-07-31 15:45:46 +0000 | [diff] [blame] | 40 | \end{funcdesc} |
| 41 | |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 42 | \begin{funcdesc}{copymode}{src, dst} |
| 43 | Copy the permission bits from \var{src} to \var{dst}. The file |
Fred Drake | 757f780 | 2001-09-04 18:26:27 +0000 | [diff] [blame] | 44 | contents, owner, and group are unaffected. \var{src} and \var{dst} |
| 45 | are path names given as strings. |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 46 | \end{funcdesc} |
| 47 | |
| 48 | \begin{funcdesc}{copystat}{src, dst} |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 49 | Copy the permission bits, last access time, last modification time, |
| 50 | and flags from \var{src} to \var{dst}. The file contents, owner, and |
Fred Drake | 757f780 | 2001-09-04 18:26:27 +0000 | [diff] [blame] | 51 | group are unaffected. \var{src} and \var{dst} are path names given |
| 52 | as strings. |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 53 | \end{funcdesc} |
| 54 | |
| 55 | \begin{funcdesc}{copy}{src, dst} |
| 56 | Copy the file \var{src} to the file or directory \var{dst}. If |
| 57 | \var{dst} is a directory, a file with the same basename as \var{src} |
| 58 | is created (or overwritten) in the directory specified. Permission |
Fred Drake | 757f780 | 2001-09-04 18:26:27 +0000 | [diff] [blame] | 59 | bits are copied. \var{src} and \var{dst} are path names given as |
| 60 | strings. |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 61 | \end{funcdesc} |
| 62 | |
| 63 | \begin{funcdesc}{copy2}{src, dst} |
| 64 | Similar to \function{copy()}, but last access time and last |
| 65 | modification time are copied as well. This is similar to the |
Fred Drake | d290c10 | 1999-11-09 18:03:00 +0000 | [diff] [blame] | 66 | \UNIX{} command \program{cp} \programopt{-p}. |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 67 | \end{funcdesc} |
| 68 | |
| 69 | \begin{funcdesc}{copytree}{src, dst\optional{, symlinks}} |
| 70 | Recursively copy an entire directory tree rooted at \var{src}. The |
| 71 | destination directory, named by \var{dst}, must not already exist; |
Johannes Gijsbers | e4172ea | 2005-01-08 12:31:29 +0000 | [diff] [blame] | 72 | it will be created as well as missing parent directories. |
| 73 | Permissions and times of directories are copied with \function{copystat()}, |
| 74 | individual files are copied using \function{copy2()}. |
| 75 | If \var{symlinks} is true, symbolic links in |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 76 | the source tree are represented as symbolic links in the new tree; |
| 77 | if false or omitted, the contents of the linked files are copied to |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 78 | the new tree. If exception(s) occur, an \exception{Error} is raised |
Neal Norwitz | 7aba3d4 | 2003-02-23 21:36:47 +0000 | [diff] [blame] | 79 | with a list of reasons. |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 80 | |
| 81 | The source code for this should be considered an example rather than |
| 82 | a tool. |
Johannes Gijsbers | e4172ea | 2005-01-08 12:31:29 +0000 | [diff] [blame] | 83 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 84 | \versionchanged[\exception{Error} is raised if any exceptions occur during |
| 85 | copying, rather than printing a message]{2.3} |
Johannes Gijsbers | e4172ea | 2005-01-08 12:31:29 +0000 | [diff] [blame] | 86 | |
| 87 | \versionchanged[Create intermediate directories needed to create \var{dst}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 88 | rather than raising an error. Copy permissions and times of |
| 89 | directories using \function{copystat()}]{2.5} |
Johannes Gijsbers | e4172ea | 2005-01-08 12:31:29 +0000 | [diff] [blame] | 90 | |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 91 | \end{funcdesc} |
| 92 | |
| 93 | \begin{funcdesc}{rmtree}{path\optional{, ignore_errors\optional{, onerror}}} |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 94 | \index{directory!deleting} |
| 95 | Delete an entire directory tree (\var{path} must point to a directory). |
| 96 | If \var{ignore_errors} is true, errors resulting from failed removals |
| 97 | will be ignored; if false or omitted, such errors are handled by |
| 98 | calling a handler specified by \var{onerror} or, if that is omitted, |
| 99 | they raise an exception. |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 100 | |
| 101 | If \var{onerror} is provided, it must be a callable that accepts |
| 102 | three parameters: \var{function}, \var{path}, and \var{excinfo}. |
| 103 | The first parameter, \var{function}, is the function which raised |
Guido van Rossum | 8cec3ab | 2004-07-14 00:48:58 +0000 | [diff] [blame] | 104 | the exception; it will be \function{os.listdir()}, \function{os.remove()} or |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 105 | \function{os.rmdir()}. The second parameter, \var{path}, will be |
| 106 | the path name passed to \var{function}. The third parameter, |
| 107 | \var{excinfo}, will be the exception information return by |
| 108 | \function{sys.exc_info()}. Exceptions raised by \var{onerror} will |
| 109 | not be caught. |
| 110 | \end{funcdesc} |
| 111 | |
Martin v. Löwis | e9ce0b0 | 2002-10-07 13:23:24 +0000 | [diff] [blame] | 112 | \begin{funcdesc}{move}{src, dst} |
| 113 | Recursively move a file or directory to another location. |
| 114 | |
| 115 | If the destination is on our current filesystem, then simply use |
| 116 | rename. Otherwise, copy src to the dst and then remove src. |
| 117 | |
| 118 | \versionadded{2.3} |
| 119 | \end{funcdesc} |
| 120 | |
| 121 | \begin{excdesc}{Error} |
| 122 | This exception collects exceptions that raised during a mult-file |
| 123 | operation. For \function{copytree}, the exception argument is a |
| 124 | list of 3-tuples (\var{srcname}, \var{dstname}, \var{exception}). |
| 125 | |
| 126 | \versionadded{2.3} |
| 127 | \end{excdesc} |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 128 | |
| 129 | \subsection{Example \label{shutil-example}} |
| 130 | |
| 131 | This example is the implementation of the \function{copytree()} |
Fred Drake | 11bc8cf | 1999-04-21 17:08:51 +0000 | [diff] [blame] | 132 | function, described above, with the docstring omitted. It |
| 133 | demonstrates many of the other functions provided by this module. |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 134 | |
| 135 | \begin{verbatim} |
| 136 | def copytree(src, dst, symlinks=0): |
| 137 | names = os.listdir(src) |
| 138 | os.mkdir(dst) |
| 139 | for name in names: |
| 140 | srcname = os.path.join(src, name) |
| 141 | dstname = os.path.join(dst, name) |
| 142 | try: |
| 143 | if symlinks and os.path.islink(srcname): |
| 144 | linkto = os.readlink(srcname) |
| 145 | os.symlink(linkto, dstname) |
| 146 | elif os.path.isdir(srcname): |
Raymond Hettinger | 550fd5d | 2002-06-30 04:43:20 +0000 | [diff] [blame] | 147 | copytree(srcname, dstname, symlinks) |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 148 | else: |
| 149 | copy2(srcname, dstname) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 150 | except (IOError, os.error) as why: |
Fred Drake | 449e18f | 1998-12-28 20:16:58 +0000 | [diff] [blame] | 151 | print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why)) |
| 152 | \end{verbatim} |