class - shutil

最後更新: 2015-10-02

介紹

offers a number of high-level operations on files and collections of files

 


Usage

 

shutil.copyfile(src, dst)

* "dst" must be the complete target file name (亦即係唔用得目錄)

* If dst already exists, it will be replaced.

 

shutil.copy(src, dst)

* If dst is a directory, a file with the same basename as src is created

* If dst already exists, it will be replaced.

ie.

import shutil
shutil.copy('test.txt','./backup/')

# * 當 folder 不存在時
# IOError: [Errno 21] Is a directory: './backup/'

shutil.copystat(src, dst)

shutil.copy2(src, dst)

metadata is copied as well
 - shutil.copy() followed by copystat()
 
 
 shutil.move(src, dst)

    Recursively move a file or directory (src) to another location (dst).

 

 shutil.rmtree(path[, ignore_errors[, onerror]])

    Delete an entire directory tree; path must point to a directory
    
 shutil.copytree(src, dst, symlinks=False, ignore=None)

    Recursively copy an entire directory tree rooted at src. The destination directory, named by dst, must not already exist;

 

Creative Commons license icon Creative Commons license icon