1 \section{\module{shutil
} ---
2 High-level file operations
}
4 \declaremodule{standard
}{shutil
}
5 \modulesynopsis{High-level file operations, including copying.
}
6 \sectionauthor{Fred L. Drake, Jr.
}{fdrake@acm.org
}
7 % partly based on the docstrings
10 The
\module{shutil
} module offers a number of high-level operations on
11 files and collections of files. In particular, functions are provided
12 which support file copying and removal.
16 \strong{Caveat:
} On MacOS, the resource fork and other metadata are
17 not used. For file copies, this means that resources will be lost and
18 file type and creator codes will not be correct.
21 \begin{funcdesc
}{copyfile
}{src, dst
}
22 Copy the contents of the file named
\var{src
} to a file named
23 \var{dst
}. If
\var{dst
} exists, it will be replaced, otherwise it
27 \begin{funcdesc
}{copyfileobj
}{fsrc, fdst
\optional{, length
}}
28 Copy the contents of the file-like object
\var{fsrc
} to the
29 file-like object
\var{fdst
}. The integer
\var{length
}, if given,
30 is the buffer size. In particular, a negative
\var{length
} value
31 means to copy the data without looping over the source data in
32 chunks; by default the data is read in chunks to avoid uncontrolled
36 \begin{funcdesc
}{copymode
}{src, dst
}
37 Copy the permission bits from
\var{src
} to
\var{dst
}. The file
38 contents, owner, and group are unaffected.
41 \begin{funcdesc
}{copystat
}{src, dst
}
42 Copy the permission bits, last access time, and last modification
43 time from
\var{src
} to
\var{dst
}. The file contents, owner, and
47 \begin{funcdesc
}{copy
}{src, dst
}
48 Copy the file
\var{src
} to the file or directory
\var{dst
}. If
49 \var{dst
} is a directory, a file with the same basename as
\var{src
}
50 is created (or overwritten) in the directory specified. Permission
54 \begin{funcdesc
}{copy2
}{src, dst
}
55 Similar to
\function{copy()
}, but last access time and last
56 modification time are copied as well. This is similar to the
57 \UNIX{} command
\program{cp
} \programopt{-p
}.
60 \begin{funcdesc
}{copytree
}{src, dst
\optional{, symlinks
}}
61 Recursively copy an entire directory tree rooted at
\var{src
}. The
62 destination directory, named by
\var{dst
}, must not already exist;
63 it will be created. Individual files are copied using
64 \function{copy2()
}. If
\var{symlinks
} is true, symbolic links in
65 the source tree are represented as symbolic links in the new tree;
66 if false or omitted, the contents of the linked files are copied to
67 the new tree. Errors are reported to standard output.
69 The source code for this should be considered an example rather than
73 \begin{funcdesc
}{rmtree
}{path
\optional{, ignore_errors
\optional{, onerror
}}}
74 \index{directory!deleting
}
75 Delete an entire directory tree. If
\var{ignore_errors
} is true,
76 errors will be ignored; if false or omitted, errors are handled by
77 calling a handler specified by
\var{onerror
} or raise an exception.
79 If
\var{onerror
} is provided, it must be a callable that accepts
80 three parameters:
\var{function
},
\var{path
}, and
\var{excinfo
}.
81 The first parameter,
\var{function
}, is the function which raised
82 the exception; it will be
\function{os.remove()
} or
83 \function{os.rmdir()
}. The second parameter,
\var{path
}, will be
84 the path name passed to
\var{function
}. The third parameter,
85 \var{excinfo
}, will be the exception information return by
86 \function{sys.exc_info()
}. Exceptions raised by
\var{onerror
} will
91 \subsection{Example
\label{shutil-example
}}
93 This example is the implementation of the
\function{copytree()
}
94 function, described above, with the docstring omitted. It
95 demonstrates many of the other functions provided by this module.
98 def copytree(src, dst, symlinks=
0):
99 names = os.listdir(src)
102 srcname = os.path.join(src, name)
103 dstname = os.path.join(dst, name)
105 if symlinks and os.path.islink(srcname):
106 linkto = os.readlink(srcname)
107 os.symlink(linkto, dstname)
108 elif os.path.isdir(srcname):
109 copytree(srcname, dstname)
111 copy2(srcname, dstname)
112 # XXX What about devices, sockets etc.?
113 except (IOError, os.error), why:
114 print "Can't copy
%s to %s: %s" % (`srcname`, `dstname`, str(why))