💥 TRENDING: /library/shutil.html - Uncensored 2025
shutil — High-level file operations¶
Source code: Lib/shutil.py
The shutil 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. For operations on individual files, see also the
os module.
Warning
Even the higher-level file copying functions (shutil.copy(),
shutil.copy2()) cannot copy all file metadata.
On POSIX platforms, this means that file owner and group are lost as well as ACLs. On Mac OS, the resource fork and other metadata are not used. This means that resources will be lost and file type and creator codes will not be correct. On Windows, file owners, ACLs and alternate data streams are not copied.
Directory and files operations¶
- shutil.copyfileobj(fsrc, fdst[, length])¶
Copy the contents of the file-like object fsrc to the file-like object fdst. The integer length, if given, is the buffer size. In particular, a negative length 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 memory consumption. Note that if the current file position of the fsrc object is not 0, only the contents from the current file position to the end of the file will be copied.
copyfileobj()will not guarantee that the destination stream has been flushed on completion of the copy. If you want to read from the destination at the completion of the copy operation (for example, reading the contents of a temporary file that has been copied from a HTTP stream), you must ensure that you have calledflush()orclose()on the file-like object before attempting to read the destination file.
- shutil.copyfile(src, dst, *, follow_symlinks=True)¶
Copy the contents (no metadata) of the file named src to a file named dst and return dst in the most efficient way possible. src and dst are path-like objects or path names given as strings.
dst must be the complete target file name; look at
copy()for a copy that accepts a target directory path. If src and dst specify the same file,SameFileErroris raised.The destination location must be writable; otherwise, an
OSErrorexception will be raised. If dst already exists, it will be replaced. Special files such as character or block devices and pipes cannot be copied with this function.If follow_symlinks is false and src is a symbolic link, a new symbolic link will be created instead of copying the file src points to.
Raises an auditing event
shutil.copyfilewith argumentssrc,dst.Changed in version 3.3:
IOErrorused to be raised instead ofOSError. Added follow_symlinks argument. Now returns dst.Changed in version 3.4: Raise
SameFileErrorinstead ofError. Since the former is a subclass of the latter, this change is backward compatible.Changed in version 3.8: Platform-specific fast-copy syscalls may be used internally in order to copy the file more efficiently. See Platform-dependent efficient copy operations section.
- exception shutil.SpecialFileError¶
This exception is raised when
copyfile()orcopytree()attempt to copy a named pipe.Added in version 2.7.
- exception shutil.SameFileError¶
This exception is raised if source and destination in
copyfile()are the same file.Added in version 3.4.
- shutil.copymode(src, dst, *, follow_symlinks=True)¶
Copy the permission bits from src to dst. The file contents, owner, and group are unaffected. src and dst are path-like objects or path names given as strings. If follow_symlinks is false, and both src and dst are symbolic links,
copymode()will attempt to modify the mode of dst itself (rather than the file it points to). This functionality is not available on every platform; please seecopystat()for more information. Ifcopymode()cannot modify symbolic links on the local platform, and it is asked to do so, it will do nothing and return.Raises an auditing event
shutil.copymodewith argumentssrc,dst.Changed in version 3.3: Added follow_symlinks argument.
- shutil.copystat(src, dst, *, follow_symlinks=True)¶
Copy the permission bits, last access time, last modification time, and flags from src to dst. On Linux,
copystat()also copies the “extended attributes” where possible. The file contents, owner, and group are unaffected. src and dst are path-like objects or path names given as strings.If follow_symlinks is false, and src and dst both refer to symbolic links,
copystat()will operate on the symbolic links themselves rather than the files the symbolic links refer to—reading the information from the src symbolic link, and writing the information to the dst symbolic link.Note
Not all platforms provide the ability to examine and modify symbolic links. Python itself can tell you what functionality is locally available.
If
os.chmod in os.supports_follow_symlinksisTrue,copystat()can modify the permission bits of a symbolic link.If
os.utime in os.supports_follow_symlinksisTrue,copystat()can modify the last access and modification times of a symbolic link.If
os.chflags in os.supports_follow_symlinksisTrue,copystat()can modify the flags of a symbolic link. (os.chflagsis not available on all platforms.)
On platforms where some or all of this functionality is unavailable, when asked to modify a symbolic link,
copystat()will copy everything it can.copystat()never returns failure.Please see
os.supports_follow_symlinksfor more information.Raises an auditing event
shutil.copystatwith argumentssrc,dst.Changed in version 3.3: Added follow_symlinks argument and support for Linux extended attributes.
- shutil.copy(src, dst, *, follow_symlinks=True)¶
Copies the file src to the file or directory dst. src and dst should be path-like objects or strings. If dst specifies a directory, the file will be copied into dst using the base filename from src. If dst specifies a file that already exists, it will be replaced. Returns the path to the newly created file.
If follow_symlinks is false, and src is a symbolic link, dst will be created as a symbolic link. If follow_symlinks is true and src is a symbolic link, dst will be a copy of the file src refers to.
copy()copies the file data and the file’s permission mode (seeos.chmod()). Other metadata, like the file’s creation and modification times, is not preserved. To preserve all file metadata from the original, usecopy2()instead.Raises an auditing event
shutil.copyfilewith argumentssrc,dst.Raises an auditing event
shutil.copymodewith argumentssrc,dst.Changed in version 3.3: Added follow_symlinks argument. Now returns path to the newly created file.
Changed in version 3.8: Platform-specific fast-copy syscalls may be used internally in order to copy the file more efficiently. See Platform-dependent efficient copy operations section.
- shutil.copy2(src, dst, *, follow_symlinks=True)¶
Identical to
copy()except thatcopy2()also attempts to preserve file metadata.When follow_symlinks is false, and src is a symbolic link,
copy2()attempts to copy all metadata from the src symbolic link to the newly created dst symbolic link. However, this functionality is not available on all platforms. On platforms where some or all of this functionality is unavailable,copy2()will preserve all the metadata it can;copy2()never raises an exception because it cannot preserve file metadata.copy2()usescopystat()to copy the file metadata. Please seecopystat()for more information about platform support for modifying symbolic link metadata.Raises an auditing event
shutil.copyfilewith argumentssrc,dst.Raises an auditing event
shutil.copystatwith argumentssrc,dst.Changed in version 3.3: Added follow_symlinks argument, try to copy extended file system attributes too (currently Linux only). Now returns path to the newly created file.
Changed in version 3.8: Platform-specific fast-copy syscalls may be used internally in order to copy the file more efficiently. See Platform-dependent efficient copy operations section.
- shutil.ignore_patterns(*patterns)¶
This factory function creates a function that can be used as a callable for
copytree()'s ignore argument, ignoring files and directories that match one of the glob-style patterns provided. See the example below.
- shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False)¶
Recursively copy an entire directory tree rooted at src to a directory named dst and return the destination directory. All intermediate directories needed to contain dst will also be created by default.
Permissions and times of directories are copied with
copystat(), individual files are copied usingcopy2().If symlinks is true, symbolic links in the source tree are represented as symbolic links in the new tree and the metadata of the original links will be copied as far as the platform allows; if false or omitted, the contents and metadata of the linked files are copied to the new tree.
When symlinks is false, if the file pointed to by the symlink doesn’t exist, an exception will be added in the list of errors raised in an
Errorexception at the end of the copy process. You can set the optional ignore_dangling_symlinks flag to true if you want to silence this exception. Notice that this option has no effect on platforms that don’t supportos.symlink().If ignore is given, it must be a callable that will receive as its arguments the directory being visited by
copytree(), and a list of its contents, as returned byos.listdir(). Sincecopytree()is called recursively, the ignore callable will be called once for each directory that is copied. The callable must return a sequence of directory and file names relative to the current directory (i.e. a subset of the items in its second argument); these names will then be ignored in the copy process.ignore_patterns()can be used to create such a callable that ignores names based on glob-style patterns.If exception(s) occur, an
Erroris raised with a list of reasons.If copy_function is given, it must be a callable that will be used to copy each file. It will be called with the source path and the destination path as arguments. By default,
copy2()is used, but any function that supports the same signature (likecopy()) can be used.If dirs_exist_ok is false (the default) and dst already exists, a
FileExistsErroris raised. If dirs_exist_ok is true, the copying operation will continue if it encounters existing directories, and files within the dst tree will be overwritten by corresponding files from the src tree.Raises an auditing event
shutil.copytreewith argumentssrc,dst.Changed in version 3.2: Added the copy_function argument to be able to provide a custom copy function. Added the ignore_dangling_symlinks argument to silence dangling symlinks errors when symlinks is false.
Changed in version 3.3: Copy metadata when symlinks is false. Now returns dst.
Changed in version 3.8: Platform-specific fast-copy syscalls may be used internally in order to copy the file more efficiently. See Platform-dependent efficient copy operations section.
Changed in version 3.8: Added the dirs_exist_ok parameter.
- shutil.rmtree(path, ignore_errors=False, onerror=None, *, onexc=None, dir_fd=None)¶
Delete an entire directory tree; path must point to a directory (but not a symbolic link to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onexc or onerror or, if both are omitted, exceptions are propagated to the caller.
This function can support paths relative to directory descriptors.
Note
On platforms that support the necessary fd-based functions a symlink attack resistant version of
rmtree()is used by default. On other platforms, thermtree()implementation is susceptible to a symlink attack: given proper timing and circumstances, attackers can manipulate symlinks on the filesystem to delete files they wouldn’t be able to access otherwise. Applications can use thermtree.avoids_symlink_attacksfunction attribute to determine which case applies.If onexc is provided, it must be a callable that accepts three parameters: function, path, and excinfo.
The first parameter, function, is the function which raised the exception; it depends on the platform and implementation. The second parameter, path, will be the path name passed to function. The third parameter, excinfo, is the exception that was raised. Exceptions raised by onexc will not be caught.
The deprecated onerror is similar to onexc, except that the third parameter it receives is the tuple returned from
sys.exc_info().See also
rmtree example for an example of handling the removal of a directory tree that contains read-only files.
Raises an auditing event
shutil.rmtreewith argumentspath,dir_fd.Changed in version 3.3: Added a symlink attack resistant version that is used automatically if platform supports fd-based functions.
Changed in version 3.8: On Windows, will no longer delete the contents of a directory junction before removing the junction.
Changed in version 3.11: Added the dir_fd parameter.
Changed in version 3.12: Added the onexc parameter, deprecated onerror.
Changed in version 3.13:
rmtree()now ignoresFileNotFoundErrorexceptions for all but the top-level path. Exceptions other thanOSErrorand subclasses ofOSErrorare now always propagated to the caller.- rmtree.avoids_symlink_attacks