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