shutil

shutil

高级的文件,文件夹,压缩包的处理模块,也主要用于文件的拷贝

将文件内容拷贝到另一个文件中

shutil.copyfileobj(fsrc, fdst[, length])

import shutil
shutil.copyfileobj(open('old.xml','r'), open('new.xml','w'))

拷贝文件

shutil.copyfile(src, dst)

shutil.copyfile('f1.log','f2.log') #目标文件无需存在

仅拷贝权限。内容、组、用户均不变

shutil.copymode(src, dst)

shutil.copymode('f1.log','f2.log')#目标文件必须存在

仅拷贝状态的信息,包括:mode bits, atime, mtime, flags

shutil.copystat(src, dst)

shutil.copystat('f1.log','f2.log')#目标文件必须存在

拷贝文件和权限

shutil.copy(src, dst)

import shutil23shutil.copy('f1.log','f2.log')

拷贝文件和状态信息

shutil.copy2(src, dst)

import shutil23shutil.copy2('f1.log','f2.log')

递归的去删除文件

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

import shutil
shutil.rmtree('folder1')

递归的去移动文件,它类似mv命令,其实就是重命名。

shutil.move(src, dst)

import shutil
shutil.move('folder1','folder3')
原文地址:https://www.cnblogs.com/kai-/p/12591901.html