blob: 3275179c4eef1bf805fa1ad73c60ed1e0465df4b [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`shutil` --- High-level file operations
3============================================
4
5.. module:: shutil
6 :synopsis: High-level file operations, including copying.
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
Christian Heimes5b5e81c2007-12-31 16:14:33 +00008.. partly based on the docstrings
Georg Brandl116aa622007-08-15 14:28:22 +00009
10.. index::
11 single: file; copying
12 single: copying files
13
14The :mod:`shutil` module offers a number of high-level operations on files and
15collections of files. In particular, functions are provided which support file
Guido van Rossum2cc30da2007-11-02 23:46:40 +000016copying and removal. For operations on individual files, see also the
17:mod:`os` module.
Georg Brandl116aa622007-08-15 14:28:22 +000018
Guido van Rossumda27fd22007-08-17 00:24:54 +000019.. warning::
20
21 On MacOS, the resource fork and other metadata are not used. For file copies,
22 this means that resources will be lost and file type and creator codes will
23 not be correct.
Georg Brandl116aa622007-08-15 14:28:22 +000024
25
26.. function:: copyfile(src, dst)
27
28 Copy the contents of the file named *src* to a file named *dst*. The
29 destination location must be writable; otherwise, an :exc:`IOError` exception
30 will be raised. If *dst* already exists, it will be replaced. Special files
31 such as character or block devices and pipes cannot be copied with this
32 function. *src* and *dst* are path names given as strings.
33
34
35.. function:: copyfileobj(fsrc, fdst[, length])
36
37 Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
38 The integer *length*, if given, is the buffer size. In particular, a negative
39 *length* value means to copy the data without looping over the source data in
40 chunks; by default the data is read in chunks to avoid uncontrolled memory
41 consumption. Note that if the current file position of the *fsrc* object is not
42 0, only the contents from the current file position to the end of the file will
43 be copied.
44
45
46.. function:: copymode(src, dst)
47
48 Copy the permission bits from *src* to *dst*. The file contents, owner, and
49 group are unaffected. *src* and *dst* are path names given as strings.
50
51
52.. function:: copystat(src, dst)
53
54 Copy the permission bits, last access time, last modification time, and flags
55 from *src* to *dst*. The file contents, owner, and group are unaffected. *src*
56 and *dst* are path names given as strings.
57
58
59.. function:: copy(src, dst)
60
61 Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a
62 file with the same basename as *src* is created (or overwritten) in the
63 directory specified. Permission bits are copied. *src* and *dst* are path
64 names given as strings.
65
66
67.. function:: copy2(src, dst)
68
69 Similar to :func:`copy`, but last access time and last modification time are
70 copied as well. This is similar to the Unix command :program:`cp -p`.
71
72
73.. function:: copytree(src, dst[, symlinks])
74
75 Recursively copy an entire directory tree rooted at *src*. The destination
76 directory, named by *dst*, must not already exist; it will be created as well as
77 missing parent directories. Permissions and times of directories are copied with
78 :func:`copystat`, individual files are copied using :func:`copy2`. If
79 *symlinks* is true, symbolic links in the source tree are represented as
80 symbolic links in the new tree; if false or omitted, the contents of the linked
81 files are copied to the new tree. If exception(s) occur, an :exc:`Error` is
82 raised with a list of reasons.
83
Georg Brandl55ac8f02007-09-01 13:51:09 +000084 The source code for this should be considered an example rather than a tool.
Georg Brandl116aa622007-08-15 14:28:22 +000085
86
87.. function:: rmtree(path[, ignore_errors[, onerror]])
88
89 .. index:: single: directory; deleting
90
91 Delete an entire directory tree (*path* must point to a directory). If
92 *ignore_errors* is true, errors resulting from failed removals will be ignored;
93 if false or omitted, such errors are handled by calling a handler specified by
94 *onerror* or, if that is omitted, they raise an exception.
95
96 If *onerror* is provided, it must be a callable that accepts three parameters:
97 *function*, *path*, and *excinfo*. The first parameter, *function*, is the
98 function which raised the exception; it will be :func:`os.listdir`,
99 :func:`os.remove` or :func:`os.rmdir`. The second parameter, *path*, will be
100 the path name passed to *function*. The third parameter, *excinfo*, will be the
101 exception information return by :func:`sys.exc_info`. Exceptions raised by
102 *onerror* will not be caught.
103
104
105.. function:: move(src, dst)
106
107 Recursively move a file or directory to another location.
108
109 If the destination is on our current filesystem, then simply use rename.
110 Otherwise, copy src to the dst and then remove src.
111
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113.. exception:: Error
114
115 This exception collects exceptions that raised during a mult-file operation. For
116 :func:`copytree`, the exception argument is a list of 3-tuples (*srcname*,
117 *dstname*, *exception*).
118
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120.. _shutil-example:
121
122Example
123-------
124
125This example is the implementation of the :func:`copytree` function, described
126above, with the docstring omitted. It demonstrates many of the other functions
127provided by this module. ::
128
129 def copytree(src, dst, symlinks=False):
130 names = os.listdir(src)
131 os.makedirs(dst)
132 errors = []
133 for name in names:
134 srcname = os.path.join(src, name)
135 dstname = os.path.join(dst, name)
136 try:
137 if symlinks and os.path.islink(srcname):
138 linkto = os.readlink(srcname)
139 os.symlink(linkto, dstname)
140 elif os.path.isdir(srcname):
141 copytree(srcname, dstname, symlinks)
142 else:
143 copy2(srcname, dstname)
144 # XXX What about devices, sockets etc.?
145 except (IOError, os.error) as why:
146 errors.append((srcname, dstname, str(why)))
147 # catch the Error from the recursive copytree so that we can
148 # continue with other files
149 except Error as err:
150 errors.extend(err.args[0])
151 try:
152 copystat(src, dst)
153 except WindowsError:
154 # can't copy file access times on Windows
155 pass
156 except OSError as why:
157 errors.extend((src, dst, str(why)))
158 if errors:
Collin Winterc79461b2007-09-01 23:34:30 +0000159 raise Error(errors)
Georg Brandl116aa622007-08-15 14:28:22 +0000160