blob: a3ab3877abd7c4d2bc8879bbf9bd1acaeee86088 [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::
Christian Heimes7f044312008-01-06 17:05:40 +000020
21 Even the higher-level file copying functions (:func:`copy`, :func:`copy2`)
22 can't copy all file metadata.
Guido van Rossumda27fd22007-08-17 00:24:54 +000023
Christian Heimes7f044312008-01-06 17:05:40 +000024 On POSIX platforms, this means that file owner and group are lost as well
25 as ACLs. On MacOS, the resource fork and other metadata are not used.
26 This means that resources will be lost and file type and creator codes will
27 not be correct. On Windows, file owners, ACLs and alternate data streams
28 are not copied.
Georg Brandl116aa622007-08-15 14:28:22 +000029
30
Georg Brandl116aa622007-08-15 14:28:22 +000031.. function:: copyfileobj(fsrc, fdst[, length])
32
33 Copy the contents of the file-like object *fsrc* to the file-like object *fdst*.
34 The integer *length*, if given, is the buffer size. In particular, a negative
35 *length* value 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 memory
37 consumption. Note that if the current file position of the *fsrc* object is not
38 0, only the contents from the current file position to the end of the file will
39 be copied.
40
41
Christian Heimesa342c012008-04-20 21:01:16 +000042.. function:: copyfile(src, dst)
43
44 Copy the contents (no metadata) of the file named *src* to a file named *dst*.
45 *dst* must be the complete target file name; look at :func:`copy` for a copy that
46 accepts a target directory path.
47 The destination location must be writable; otherwise, an :exc:`IOError` exception
48 will be raised. If *dst* already exists, it will be replaced. Special files
49 such as character or block devices and pipes cannot be copied with this
50 function. *src* and *dst* are path names given as strings.
51
52
Georg Brandl116aa622007-08-15 14:28:22 +000053.. function:: copymode(src, dst)
54
55 Copy the permission bits from *src* to *dst*. The file contents, owner, and
56 group are unaffected. *src* and *dst* are path names given as strings.
57
58
59.. function:: copystat(src, dst)
60
61 Copy the permission bits, last access time, last modification time, and flags
62 from *src* to *dst*. The file contents, owner, and group are unaffected. *src*
63 and *dst* are path names given as strings.
64
65
66.. function:: copy(src, dst)
67
68 Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a
69 file with the same basename as *src* is created (or overwritten) in the
70 directory specified. Permission bits are copied. *src* and *dst* are path
71 names given as strings.
72
73
74.. function:: copy2(src, dst)
75
76 Similar to :func:`copy`, but last access time and last modification time are
77 copied as well. This is similar to the Unix command :program:`cp -p`.
78
79
80.. function:: copytree(src, dst[, symlinks])
81
82 Recursively copy an entire directory tree rooted at *src*. The destination
83 directory, named by *dst*, must not already exist; it will be created as well as
84 missing parent directories. Permissions and times of directories are copied with
85 :func:`copystat`, individual files are copied using :func:`copy2`. If
86 *symlinks* is true, symbolic links in the source tree are represented as
87 symbolic links in the new tree; if false or omitted, the contents of the linked
88 files are copied to the new tree. If exception(s) occur, an :exc:`Error` is
89 raised with a list of reasons.
90
Georg Brandl55ac8f02007-09-01 13:51:09 +000091 The source code for this should be considered an example rather than a tool.
Georg Brandl116aa622007-08-15 14:28:22 +000092
93
94.. function:: rmtree(path[, ignore_errors[, onerror]])
95
96 .. index:: single: directory; deleting
97
Christian Heimes9bd667a2008-01-20 15:14:11 +000098 Delete an entire directory tree; *path* must point to a directory (but not a
99 symbolic link to a directory). If *ignore_errors* is true, errors resulting
100 from failed removals will be ignored; if false or omitted, such errors are
101 handled by calling a handler specified by *onerror* or, if that is omitted,
102 they raise an exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Christian Heimes9bd667a2008-01-20 15:14:11 +0000104 If *onerror* is provided, it must be a callable that accepts three
105 parameters: *function*, *path*, and *excinfo*. The first parameter,
106 *function*, is the function which raised the exception; it will be
107 :func:`os.path.islink`, :func:`os.listdir`, :func:`os.remove` or
108 :func:`os.rmdir`. The second parameter, *path*, will be the path name passed
109 to *function*. The third parameter, *excinfo*, will be the exception
110 information return by :func:`sys.exc_info`. Exceptions raised by *onerror*
111 will not be caught.
112
Georg Brandl116aa622007-08-15 14:28:22 +0000113
114.. function:: move(src, dst)
115
116 Recursively move a file or directory to another location.
117
Christian Heimes7f044312008-01-06 17:05:40 +0000118 If the destination is on the current filesystem, then simply use rename.
Georg Brandl116aa622007-08-15 14:28:22 +0000119 Otherwise, copy src to the dst and then remove src.
120
Georg Brandl116aa622007-08-15 14:28:22 +0000121
122.. exception:: Error
123
Christian Heimes7f044312008-01-06 17:05:40 +0000124 This exception collects exceptions that raised during a multi-file operation. For
Georg Brandl116aa622007-08-15 14:28:22 +0000125 :func:`copytree`, the exception argument is a list of 3-tuples (*srcname*,
126 *dstname*, *exception*).
127
Georg Brandl116aa622007-08-15 14:28:22 +0000128
129.. _shutil-example:
130
131Example
132-------
133
134This example is the implementation of the :func:`copytree` function, described
135above, with the docstring omitted. It demonstrates many of the other functions
136provided by this module. ::
137
138 def copytree(src, dst, symlinks=False):
139 names = os.listdir(src)
140 os.makedirs(dst)
141 errors = []
142 for name in names:
143 srcname = os.path.join(src, name)
144 dstname = os.path.join(dst, name)
145 try:
146 if symlinks and os.path.islink(srcname):
147 linkto = os.readlink(srcname)
148 os.symlink(linkto, dstname)
149 elif os.path.isdir(srcname):
150 copytree(srcname, dstname, symlinks)
151 else:
152 copy2(srcname, dstname)
153 # XXX What about devices, sockets etc.?
154 except (IOError, os.error) as why:
155 errors.append((srcname, dstname, str(why)))
156 # catch the Error from the recursive copytree so that we can
157 # continue with other files
158 except Error as err:
159 errors.extend(err.args[0])
160 try:
161 copystat(src, dst)
162 except WindowsError:
163 # can't copy file access times on Windows
164 pass
165 except OSError as why:
166 errors.extend((src, dst, str(why)))
167 if errors:
Collin Winterc79461b2007-09-01 23:34:30 +0000168 raise Error(errors)
Georg Brandl116aa622007-08-15 14:28:22 +0000169