shutil有很多方法可以使用。其中之一是:
from shutil import copyfile
copyfile(src, dst)将名为src的文件的内容复制到名为dst的文件中。目的地位置必须是可写的; 否则,将IOError异常。如果dst已经存在,它将被替换。使用此功能无法复制特殊文件,如字符或块设备和管道。 src和dst是以字符串形式给出的路径名。
-----------------------------------------------------------------------------------
| Function |Copies Metadata|Copies Permissions|Can Use Buffer|Dest Dir OK
-----------------------------------------------------------------------------------
| shutil.copy | No | Yes | No | Yes
-----------------------------------------------------------------------------------
| shutil.copyfile | No | No | No | No
-----------------------------------------------------------------------------------
| shutil.copy2 | Yes | Yes | No | Yes
-----------------------------------------------------------------------------------
| shutil.copyfileobj| No | No | Yes | No
-----------------------------------------------------------------------------------
copy2(src,dst)通常比copyfile(src,dst)更有用copyfile(src,dst)因为:
dst成为一个目录 (而不是完整的目标文件名),在这种情况下, src的基本名称用于创建新文件; 这是一个简短的例子:
import shutil
shutil.copy2('/src/dir/file.ext', '/dst/dir/newname.ext') # complete target filename given
shutil.copy2('/src/file.ext', '/dst/dir') # target filename is /dst/dir/file.ext