Python 之 shutil模块

2023-01-31 05:01:47 python shutil 模块

shutil模块用于执行高级的文件操作,如复制,移动重命名等。

>>> shutil.
shutil.Error             shutil._samefile
shutil.__all__           shutil.abspath
shutil.__class__         shutil.copy
shutil.__delattr__       shutil.copy2
shutil.__dict__          shutil.copyfile
shutil.__doc__           shutil.copyfileobj
shutil.__file__          shutil.copymode
shutil.__getattribute__  shutil.copystat
shutil.__hash__          shutil.copytree
shutil.__init__          shutil.destinsrc
shutil.__name__          shutil.exceptions
shutil.__new__           shutil.move
shutil.__reduce__        shutil.os
shutil.__reduce_ex__     shutil.rmtree
shutil.__repr__          shutil.stat
shutil.__setattr__       shutil.sys
shutil.__str__

copy(src, dst):复制文件

>>> shutil.copy('/root/zhu.txt','/root/tao.txt')
>>> shutil.copy('/root/zhu.txt','/root/zhuzhu')
copy(src, dst) :#对文件进行复制,src必须是文件,而dst可以是目录。

copy2(src, dst):连同权限一起复制,相当于cp -p src dst

>>> shutil.copy2('/root/zhu.txt','/root/zhuzhu')


copyfile(src, dst):dst只能是文件,

>>> shutil.copyfile('/root/zhu.txt','/root/zhuzhu')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/shutil.py", line 48, in copyfile
    fdst = open(dst, 'wb')
IOError: [Errno 21] Is a directory: '/root/zhuzhu'

move(src, dst) :移动

>>> shutil.move('/root/zhu.txt','/root/zhuzhu')
>>> help(shutil.move)
>>> help(shutil.move)
>>> shutil.move('/root/1.txt','/root/zhuzhu/gg.txt')

rmtree(path):删除整个目录

>>> shutil.rmtree('/root/zhuzhu')




























































相关文章