Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:6.22 fnmatchUp:6. Generic Operating SystemNext:6.23.1 Example

6.23shutil -- High-level file operations

Theshutil module offers a number of high-level operations onfiles and collections of files. In particular, functions are provided which support file copying and removal. 

Caveat: On MacOS, the resource fork and other metadata arenot used. For file copies, this means that resources will be lost and file type and creator codes will not be correct.

copyfile(src, dst)
Copy the contents of the file namedsrc to a file nameddst. Ifdst exists, it will be replaced, otherwise it will be created. Special files such as character or block devices and pipes cannot not be copied with this function.src anddst are path names given as strings.

copyfileobj(fsrc, fdst[, length])
Copy the contents of the file-like objectfsrc to the file-like objectfdst. The integerlength, if given, is the buffer size. In particular, a negativelength 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.

copymode(src, dst)
Copy the permission bits fromsrc todst. The file contents, owner, and group are unaffected.src anddst are path names given as strings.

copystat(src, dst)
Copy the permission bits, last access time, and last modification time fromsrc todst. The file contents, owner, and group are unaffected.src anddst are path names given as strings.

copy(src, dst)
Copy the filesrc to the file or directorydst. Ifdst is a directory, a file with the same basename assrc is created (or overwritten) in the directory specified. Permission bits are copied.src anddst are path names given as strings.

copy2(src, dst)
Similar tocopy(), but last access time and last modification time are copied as well. This is similar to the Unix commandcp-p.

copytree(src, dst[, symlinks])
Recursively copy an entire directory tree rooted atsrc. The destination directory, named bydst, must not already exist; it will be created. Individual files are copied usingcopy2(). Ifsymlinks is true, symbolic links in the source tree are represented as symbolic links in the new tree; if false or omitted, the contents of the linked files are copied to the new tree. Errors are reported to standard output.

The source code for this should be considered an example rather than a tool.

rmtree(path[, ignore_errors[, onerror]])
  Delete an entire directory tree. Ifignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified byonerror or, if that is omitted, they raise an exception.

Ifonerror is provided, it must be a callable that accepts three parameters:function,path, andexcinfo. The first parameter,function, is the function which raised the exception; it will beos.remove() oros.rmdir(). The second parameter,path, will be the path name passed tofunction. The third parameter,excinfo, will be the exception information return bysys.exc_info(). Exceptions raised byonerror will not be caught.


Subsections


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:6.22 fnmatchUp:6. Generic Operating SystemNext:6.23.1 Example
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp