| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="module-locale.html" /> |
| 13 | <link rel="prev" href="module-fnmatch.html" /> |
| 14 | <link rel="parent" href="allos.html" /> |
| 15 | <link rel="next" href="shutil-example.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>6.26 shutil -- High-level file operations</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="6.25 fnmatch " |
| 25 | href="module-fnmatch.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="6. Generic Operating System" |
| 28 | href="allos.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="6.26.1 Example" |
| 31 | href="shutil-example.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-fnmatch.html">6.25 fnmatch </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="allos.html">6. Generic Operating System</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="shutil-example.html">6.26.1 Example</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION0082600000000000000000"> |
| 56 | 6.26 <tt class="module">shutil</tt> -- |
| 57 | High-level file operations</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-shutil"></A> |
| 62 | |
| 63 | <P> |
| 64 | The <tt class="module">shutil</tt> module offers a number of high-level operations on |
| 65 | files and collections of files. In particular, functions are provided |
| 66 | which support file copying and removal. |
| 67 | <a id='l2h-2359' xml:id='l2h-2359'></a> |
| 68 | |
| 69 | <P> |
| 70 | <strong>Caveat:</strong> On MacOS, the resource fork and other metadata are |
| 71 | not used. For file copies, this means that resources will be lost and |
| 72 | file type and creator codes will not be correct. |
| 73 | |
| 74 | <P> |
| 75 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 76 | <td><nobr><b><tt id='l2h-2349' xml:id='l2h-2349' class="function">copyfile</tt></b>(</nobr></td> |
| 77 | <td><var>src, dst</var>)</td></tr></table></dt> |
| 78 | <dd> |
| 79 | Copy the contents of the file named <var>src</var> to a file named |
| 80 | <var>dst</var>. The destination location must be writable; otherwise, |
| 81 | an <tt class="exception">IOError</tt> exception will be raised. |
| 82 | If <var>dst</var> already exists, it will be replaced. |
| 83 | Special files such as character or block devices |
| 84 | and pipes cannot be copied with this function. <var>src</var> and |
| 85 | <var>dst</var> are path names given as strings. |
| 86 | </dl> |
| 87 | |
| 88 | <P> |
| 89 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 90 | <td><nobr><b><tt id='l2h-2350' xml:id='l2h-2350' class="function">copyfileobj</tt></b>(</nobr></td> |
| 91 | <td><var>fsrc, fdst</var><big>[</big><var>, length</var><big>]</big><var></var>)</td></tr></table></dt> |
| 92 | <dd> |
| 93 | Copy the contents of the file-like object <var>fsrc</var> to the |
| 94 | file-like object <var>fdst</var>. The integer <var>length</var>, if given, |
| 95 | is the buffer size. In particular, a negative <var>length</var> value |
| 96 | means to copy the data without looping over the source data in |
| 97 | chunks; by default the data is read in chunks to avoid uncontrolled |
| 98 | memory consumption. |
| 99 | </dl> |
| 100 | |
| 101 | <P> |
| 102 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 103 | <td><nobr><b><tt id='l2h-2351' xml:id='l2h-2351' class="function">copymode</tt></b>(</nobr></td> |
| 104 | <td><var>src, dst</var>)</td></tr></table></dt> |
| 105 | <dd> |
| 106 | Copy the permission bits from <var>src</var> to <var>dst</var>. The file |
| 107 | contents, owner, and group are unaffected. <var>src</var> and <var>dst</var> |
| 108 | are path names given as strings. |
| 109 | </dl> |
| 110 | |
| 111 | <P> |
| 112 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 113 | <td><nobr><b><tt id='l2h-2352' xml:id='l2h-2352' class="function">copystat</tt></b>(</nobr></td> |
| 114 | <td><var>src, dst</var>)</td></tr></table></dt> |
| 115 | <dd> |
| 116 | Copy the permission bits, last access time, and last modification |
| 117 | time from <var>src</var> to <var>dst</var>. The file contents, owner, and |
| 118 | group are unaffected. <var>src</var> and <var>dst</var> are path names given |
| 119 | as strings. |
| 120 | </dl> |
| 121 | |
| 122 | <P> |
| 123 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 124 | <td><nobr><b><tt id='l2h-2353' xml:id='l2h-2353' class="function">copy</tt></b>(</nobr></td> |
| 125 | <td><var>src, dst</var>)</td></tr></table></dt> |
| 126 | <dd> |
| 127 | Copy the file <var>src</var> to the file or directory <var>dst</var>. If |
| 128 | <var>dst</var> is a directory, a file with the same basename as <var>src</var> |
| 129 | is created (or overwritten) in the directory specified. Permission |
| 130 | bits are copied. <var>src</var> and <var>dst</var> are path names given as |
| 131 | strings. |
| 132 | </dl> |
| 133 | |
| 134 | <P> |
| 135 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 136 | <td><nobr><b><tt id='l2h-2354' xml:id='l2h-2354' class="function">copy2</tt></b>(</nobr></td> |
| 137 | <td><var>src, dst</var>)</td></tr></table></dt> |
| 138 | <dd> |
| 139 | Similar to <tt class="function">copy()</tt>, but last access time and last |
| 140 | modification time are copied as well. This is similar to the |
| 141 | <span class="Unix">Unix</span> command <b class="program">cp</b> <b class="programopt">-p</b>. |
| 142 | </dl> |
| 143 | |
| 144 | <P> |
| 145 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 146 | <td><nobr><b><tt id='l2h-2355' xml:id='l2h-2355' class="function">copytree</tt></b>(</nobr></td> |
| 147 | <td><var>src, dst</var><big>[</big><var>, symlinks</var><big>]</big><var></var>)</td></tr></table></dt> |
| 148 | <dd> |
| 149 | Recursively copy an entire directory tree rooted at <var>src</var>. The |
| 150 | destination directory, named by <var>dst</var>, must not already exist; |
| 151 | it will be created. Individual files are copied using |
| 152 | <tt class="function">copy2()</tt>. If <var>symlinks</var> is true, symbolic links in |
| 153 | the source tree are represented as symbolic links in the new tree; |
| 154 | if false or omitted, the contents of the linked files are copied to |
| 155 | the new tree. If exception(s) occur, an Error is raised |
| 156 | with a list of reasons. |
| 157 | |
| 158 | <P> |
| 159 | The source code for this should be considered an example rather than |
| 160 | a tool. |
| 161 | |
| 162 | <span class="versionnote">Changed in version 2.3: |
| 163 | Error is raised if any exceptions occur during copying, |
| 164 | rather than printing a message.</span> |
| 165 | |
| 166 | </dl> |
| 167 | |
| 168 | <P> |
| 169 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 170 | <td><nobr><b><tt id='l2h-2356' xml:id='l2h-2356' class="function">rmtree</tt></b>(</nobr></td> |
| 171 | <td><var>path</var><big>[</big><var>, ignore_errors</var><big>[</big><var>, onerror</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 172 | <dd> |
| 173 | Delete an entire directory tree.<a id='l2h-2360' xml:id='l2h-2360'></a> |
| 174 | If <var>ignore_errors</var> is true, |
| 175 | errors resulting from failed removals will be ignored; if false or |
| 176 | omitted, such errors are handled by calling a handler specified by |
| 177 | <var>onerror</var> or, if that is omitted, they raise an exception. |
| 178 | |
| 179 | <P> |
| 180 | If <var>onerror</var> is provided, it must be a callable that accepts |
| 181 | three parameters: <var>function</var>, <var>path</var>, and <var>excinfo</var>. |
| 182 | The first parameter, <var>function</var>, is the function which raised |
| 183 | the exception; it will be <tt class="function">os.listdir()</tt>, <tt class="function">os.remove()</tt> or |
| 184 | <tt class="function">os.rmdir()</tt>. The second parameter, <var>path</var>, will be |
| 185 | the path name passed to <var>function</var>. The third parameter, |
| 186 | <var>excinfo</var>, will be the exception information return by |
| 187 | <tt class="function">sys.exc_info()</tt>. Exceptions raised by <var>onerror</var> will |
| 188 | not be caught. |
| 189 | </dl> |
| 190 | |
| 191 | <P> |
| 192 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 193 | <td><nobr><b><tt id='l2h-2357' xml:id='l2h-2357' class="function">move</tt></b>(</nobr></td> |
| 194 | <td><var>src, dst</var>)</td></tr></table></dt> |
| 195 | <dd> |
| 196 | Recursively move a file or directory to another location. |
| 197 | |
| 198 | <P> |
| 199 | If the destination is on our current filesystem, then simply use |
| 200 | rename. Otherwise, copy src to the dst and then remove src. |
| 201 | |
| 202 | <P> |
| 203 | |
| 204 | <span class="versionnote">New in version 2.3.</span> |
| 205 | |
| 206 | </dl> |
| 207 | |
| 208 | <P> |
| 209 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-2358' xml:id='l2h-2358' class="exception">Error</tt></b></dt> |
| 210 | <dd> |
| 211 | This exception collects exceptions that raised during a mult-file |
| 212 | operation. For <tt class="function">copytree</tt>, the exception argument is a |
| 213 | list of 3-tuples (<var>srcname</var>, <var>dstname</var>, <var>exception</var>). |
| 214 | |
| 215 | <P> |
| 216 | |
| 217 | <span class="versionnote">New in version 2.3.</span> |
| 218 | |
| 219 | </dd></dl> |
| 220 | |
| 221 | <P> |
| 222 | |
| 223 | <p><br /></p><hr class='online-navigation' /> |
| 224 | <div class='online-navigation'> |
| 225 | <!--Table of Child-Links--> |
| 226 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> |
| 227 | |
| 228 | <UL CLASS="ChildLinks"> |
| 229 | <LI><A href="shutil-example.html">6.26.1 Example</a> |
| 230 | </ul> |
| 231 | <!--End of Table of Child-Links--> |
| 232 | </div> |
| 233 | |
| 234 | <DIV CLASS="navigation"> |
| 235 | <div class='online-navigation'> |
| 236 | <p></p><hr /> |
| 237 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 238 | <tr> |
| 239 | <td class='online-navigation'><a rel="prev" title="6.25 fnmatch " |
| 240 | href="module-fnmatch.html"><img src='../icons/previous.png' |
| 241 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 242 | <td class='online-navigation'><a rel="parent" title="6. Generic Operating System" |
| 243 | href="allos.html"><img src='../icons/up.png' |
| 244 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 245 | <td class='online-navigation'><a rel="next" title="6.26.1 Example" |
| 246 | href="shutil-example.html"><img src='../icons/next.png' |
| 247 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 248 | <td align="center" width="100%">Python Library Reference</td> |
| 249 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 250 | href="contents.html"><img src='../icons/contents.png' |
| 251 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 252 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 253 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 254 | <td class='online-navigation'><a rel="index" title="Index" |
| 255 | href="genindex.html"><img src='../icons/index.png' |
| 256 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 257 | </tr></table> |
| 258 | <div class='online-navigation'> |
| 259 | <b class="navlabel">Previous:</b> |
| 260 | <a class="sectref" rel="prev" href="module-fnmatch.html">6.25 fnmatch </A> |
| 261 | <b class="navlabel">Up:</b> |
| 262 | <a class="sectref" rel="parent" href="allos.html">6. Generic Operating System</A> |
| 263 | <b class="navlabel">Next:</b> |
| 264 | <a class="sectref" rel="next" href="shutil-example.html">6.26.1 Example</A> |
| 265 | </div> |
| 266 | </div> |
| 267 | <hr /> |
| 268 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 269 | </DIV> |
| 270 | <!--End of Navigation Panel--> |
| 271 | <ADDRESS> |
| 272 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 273 | </ADDRESS> |
| 274 | </BODY> |
| 275 | </HTML> |