<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"module-locale.html" />
<link rel=
"prev" href=
"module-fnmatch.html" />
<link rel=
"parent" href=
"allos.html" />
<link rel=
"next" href=
"shutil-example.html" />
<meta name='aesop' content='information'
/>
<title>6.26 shutil -- High-level file operations
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.25 fnmatch "
href=
"module-fnmatch.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6. Generic Operating System"
href=
"allos.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.26.1 Example"
href=
"shutil-example.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-fnmatch.html">6.25 fnmatch
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"allos.html">6. Generic Operating System
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"shutil-example.html">6.26.1 Example
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION0082600000000000000000">
6.26 <tt class=
"module">shutil
</tt> --
High-level file operations
</A>
<A NAME=
"module-shutil"></A>
The
<tt class=
"module">shutil
</tt> module offers a number of high-level operations on
files and collections of files. In particular, functions are provided
which support file copying and removal.
<a id='l2h-
2359' xml:id='l2h-
2359'
></a>
<strong>Caveat:
</strong> On MacOS, the resource fork and other metadata are
not used. For file copies, this means that resources will be lost and
file type and creator codes will not be correct.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2349' xml:id='l2h-
2349'
class=
"function">copyfile
</tt></b>(
</nobr></td>
<td><var>src, dst
</var>)
</td></tr></table></dt>
Copy the contents of the file named
<var>src
</var> to a file named
<var>dst
</var>. The destination location must be writable; otherwise,
an
<tt class=
"exception">IOError
</tt> exception will be raised.
If
<var>dst
</var> already exists, it will be replaced.
Special files such as character or block devices
and pipes cannot be copied with this function.
<var>src
</var> and
<var>dst
</var> are path names given as strings.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2350' xml:id='l2h-
2350'
class=
"function">copyfileobj
</tt></b>(
</nobr></td>
<td><var>fsrc, fdst
</var><big>[
</big><var>, length
</var><big>]
</big><var></var>)
</td></tr></table></dt>
Copy the contents of the file-like object
<var>fsrc
</var> to the
file-like object
<var>fdst
</var>. The integer
<var>length
</var>, if given,
is the buffer size. In particular, a negative
<var>length
</var> value
means to copy the data without looping over the source data in
chunks; by default the data is read in chunks to avoid uncontrolled
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2351' xml:id='l2h-
2351'
class=
"function">copymode
</tt></b>(
</nobr></td>
<td><var>src, dst
</var>)
</td></tr></table></dt>
Copy the permission bits from
<var>src
</var> to
<var>dst
</var>. The file
contents, owner, and group are unaffected.
<var>src
</var> and
<var>dst
</var>
are path names given as strings.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2352' xml:id='l2h-
2352'
class=
"function">copystat
</tt></b>(
</nobr></td>
<td><var>src, dst
</var>)
</td></tr></table></dt>
Copy the permission bits, last access time, and last modification
time from
<var>src
</var> to
<var>dst
</var>. The file contents, owner, and
group are unaffected.
<var>src
</var> and
<var>dst
</var> are path names given
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2353' xml:id='l2h-
2353'
class=
"function">copy
</tt></b>(
</nobr></td>
<td><var>src, dst
</var>)
</td></tr></table></dt>
Copy the file
<var>src
</var> to the file or directory
<var>dst
</var>. If
<var>dst
</var> is a directory, a file with the same basename as
<var>src
</var>
is created (or overwritten) in the directory specified. Permission
bits are copied.
<var>src
</var> and
<var>dst
</var> are path names given as
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2354' xml:id='l2h-
2354'
class=
"function">copy2
</tt></b>(
</nobr></td>
<td><var>src, dst
</var>)
</td></tr></table></dt>
Similar to
<tt class=
"function">copy()
</tt>, but last access time and last
modification time are copied as well. This is similar to the
<span class=
"Unix">Unix
</span> command
<b class=
"program">cp
</b> <b class=
"programopt">-p
</b>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2355' xml:id='l2h-
2355'
class=
"function">copytree
</tt></b>(
</nobr></td>
<td><var>src, dst
</var><big>[
</big><var>, symlinks
</var><big>]
</big><var></var>)
</td></tr></table></dt>
Recursively copy an entire directory tree rooted at
<var>src
</var>. The
destination directory, named by
<var>dst
</var>, must not already exist;
it will be created. Individual files are copied using
<tt class=
"function">copy2()
</tt>. If
<var>symlinks
</var> is true, symbolic links in
the source tree are represented as symbolic links in the new tree;
if false or omitted, the contents of the linked files are copied to
the new tree. If exception(s) occur, an Error is raised
The source code for this should be considered an example rather than
<span class=
"versionnote">Changed in version
2.3:
Error is raised if any exceptions occur during copying,
rather than printing a message.
</span>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2356' xml:id='l2h-
2356'
class=
"function">rmtree
</tt></b>(
</nobr></td>
<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>
Delete an entire directory tree.
<a id='l2h-
2360' xml:id='l2h-
2360'
></a>
If
<var>ignore_errors
</var> is true,
errors resulting from failed removals will be ignored; if false or
omitted, such errors are handled by calling a handler specified by
<var>onerror
</var> or, if that is omitted, they raise an exception.
If
<var>onerror
</var> is provided, it must be a callable that accepts
three parameters:
<var>function
</var>,
<var>path
</var>, and
<var>excinfo
</var>.
The first parameter,
<var>function
</var>, is the function which raised
the exception; it will be
<tt class=
"function">os.listdir()
</tt>,
<tt class=
"function">os.remove()
</tt> or
<tt class=
"function">os.rmdir()
</tt>. The second parameter,
<var>path
</var>, will be
the path name passed to
<var>function
</var>. The third parameter,
<var>excinfo
</var>, will be the exception information return by
<tt class=
"function">sys.exc_info()
</tt>. Exceptions raised by
<var>onerror
</var> will
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2357' xml:id='l2h-
2357'
class=
"function">move
</tt></b>(
</nobr></td>
<td><var>src, dst
</var>)
</td></tr></table></dt>
Recursively move a file or directory to another location.
If the destination is on our current filesystem, then simply use
rename. Otherwise, copy src to the dst and then remove src.
<span class=
"versionnote">New in version
2.3.
</span>
<dl><dt><b><span class=
"typelabel">exception
</span> <tt id='l2h-
2358' xml:id='l2h-
2358'
class=
"exception">Error
</tt></b></dt>
This exception collects exceptions that raised during a mult-file
operation. For
<tt class=
"function">copytree
</tt>, the exception argument is a
list of
3-tuples (
<var>srcname
</var>,
<var>dstname
</var>,
<var>exception
</var>).
<span class=
"versionnote">New in version
2.3.
</span>
<p><br /></p><hr class='online-navigation'
/>
<div class='online-navigation'
>
<!--Table of Child-Links-->
<A NAME=
"CHILD_LINKS"><STRONG>Subsections
</STRONG></a>
<LI><A href=
"shutil-example.html">6.26.1 Example
</a>
<!--End of Table of Child-Links-->
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.25 fnmatch "
href=
"module-fnmatch.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6. Generic Operating System"
href=
"allos.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.26.1 Example"
href=
"shutil-example.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-fnmatch.html">6.25 fnmatch
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"allos.html">6. Generic Operating System
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"shutil-example.html">6.26.1 Example
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.