python中shutil模块学习笔记



import shutil
 
一、shutil.copyfile( src, dst)  #src复制到dst中去。当然前提是目标地址是具备可写权限。抛出的异常信息为
IOException. 如果当前的dst已存在的话就会被覆盖掉 。自己测试验证了下,是src和dst必须是文件,不可以是目录



尝试结果如下:


python中shutil模块学习笔记_第1张图片



情况1:src为文件,dst为目录,报错如下:


>>> shutil.copyfile("d:/预习预习/1f","d:/预习预习/aa")
Traceback (most recent call last):
  File "", line 1, in
  File "D:\Python27\lib\shutil.py", line 97, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: 'd:/\xd4\xa4\xcf\xb0\xd4\xa4\xcf\xb0/aa'


情况2:src和dst均为文件,则正确执行,且可以执行两次,说明如果dst存在则覆盖



>>> shutil.copyfile("d:/预习预习/1f","d:/预习预习/aa/1f")


情况3:报错:
>>> shutil.copyfile("d:/预习预习/1f","d:/预习预习/aa/.")
Traceback (most recent call last):
  File "", line 1, in
  File "D:\Python27\lib\shutil.py", line 97, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: 'd:/\xd4\xa4\xcf\xb0\xd4\xa4\xcf\xb0/aa/.
'


情况4:如果srv和dst均为目录的情况 ,报错:


>>> shutil.copyfile("d:/预习预习/aa","d:/预习预习/bb")
Traceback (most recent call last):
  File "", line 1, in
  File "D:\Python27\lib\shutil.py", line 96, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 13] Permission denied: 'd:/\xd4\xa4\xcf\xb0\xd4\xa4\xcf\xb0/aa'
>>>


二、shutil.move( src, dst) #移动文件、目录。或者文件、目录重命名。如果dst存在,则不可覆盖




情况1:把文件1f移动到aa目录下,但aa目录下已经存在文件1f,则报错。



>>> shutil.move("d:/预习预习/1f","d:/预习预习/aa")
Traceback (most recent call last):
  File "", line 1, in
  File "D:\Python27\lib\shutil.py", line 306, in move
    raise Error, "Destination path '%s' already exists" % real_dst
shutil.Error: Destination path 'd:/预习预习/aa\1f' already exists


情况2:删除aa目录下的1f文件后,移动成功

>>> os.remove("d:/预习预习/aa/1f")
>>> os.listdir("d:/预习预习/aa")
['ss.txt']
>>> shutil.move("d:/预习预习/1f","d:/预习预习/aa")
>>> os.listdir("d:/预习预习/aa")
['1f', 'ss.txt']


情况3:把目录名aa改名为bb,成功



>>> shutil.move("d:/预习预习/aa","d:/预习预习/bb")
>>> os.listdir("d:/预习预习")
['1d', '20180104', 'bb', '\xce\xd2\xb5\xc4os\xd4\xa4\xcf\xb0']
>>> os.listdir("d:/预习预习/bb")
['1f', 'ss.txt']




情况4:把文件1f改名为2f


>>> shutil.move("d:/预习预习/bb/1f","d:/预习预习/bb/2f")
>>> os.listdir("d:/预习预习/bb")
['2f', 'ss.txt']
>>>




三、shutil.copy( src, dst) 复制一个文件到一个文件或一个目录 ,注意:src必须为文件,dst为文件或目录


python中shutil模块学习笔记_第2张图片 python中shutil模块学习笔记_第3张图片







情况1:src为文件,dst为目录

>>> shutil.copy("d:/预习预习/bb/2f","d:/预习预习")
>>> os.listdir("d:/预习预习/bb")
['2f', 'ss.txt']
>>> os.listdir("d:/预习预习")
['1d', '20180104', '2f', 'bb', '\xce\xd2\xb5\xc4os\xd4\xa4\xcf\xb0']
>>>


情况2:src为文件,dst为文件


>>> shutil.copy("d:/预习预习/bb/2f","d:/预习预习/3f")
>>> os.listdir("d:/预习预习")
['1d', '20180104', '2f', '3f', 'bb', '\xce\xd2\xb5\xc4os\xd4\xa4\xcf\xb
>>>




情况3:src为目录,dst为目录。报错


>>> os.listdir("d:/预习预习")
['1d', '20180104', '2f', '3f', 'bb', '\xce\xd2\xb5\xc4os\xd4\xa4\xcf\xb
>>> shutil.copy("d:/预习预习/bb","d:/预习预习/cc")
Traceback (most recent call last):
  File "", line 1, in
  File "D:\Python27\lib\shutil.py", line 133, in copy
    copyfile(src, dst)
  File "D:\Python27\lib\shutil.py", line 96, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 13] Permission denied: 'd:/\xd4\xa4\xcf\xb0\xd4\xa4\xcf
>>>


四、shutil.copy2( src, dst) #在copy上的基础上再复制文件最后访问时间与修改时间也复制过来了,类似于cp –p的东西。但是创建时间是不会和原文件一样的


>>> os.stat("d:/预习预习/3f")
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid
=0, st_size=12L, st_atime=1515591354L, st_mtime=1515591354L,st_ctime=1515591354
L)


>>> shutil.copy("d:/预习预习/3f","d:/预习预习/4f")
>>> os.stat("d:/预习预习/4f")
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid
=0, st_size=12L, st_atime=1515591668L, st_mtime=1515591668L,st_ctime=1515591668
L)

>>> shutil.copy2("d:/预习预习/3f","d:/预习预习/4f")
>>> os.stat("d:/预习预习/4f")
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid
=0, st_size=12L, st_atime=1515591354L, st_mtime=1515591354L,st_ctime=1515591668
L)

>>>


五、shutil.copytree( olddir, newdir, True/Flase) #把olddir拷贝一份newdir,如果第3个参数是True,则复制目录时将保持文件夹下的符号连接,如果第3个参数是False,则将在复制的目录下生成物理副本来替代符号连接

注意以下结果:(1)拷贝后的文件夹的最后访问时间、修改时间都是与原文件夹一样的,只有创建时间是不一样的。另外个人没有看出最后一位True和False有什么区别  (2)newdir存在的话,是不可覆盖的

>>> shutil.copytree("d:/预习预习","d:/ycyc")
>>> os.listdir("d:/预习预习")
['1d', '20180104', '2f', '3f', '4f', 'bb', '\xce\xd2\xb5\xc4os\xd4\xa4\xcf\xb0']




>>> os.listdir("d:/ycyc")
['1d', '20180104', '2f', '3f', '4f', 'bb', '\xce\xd2\xb5\xc4os\xd4\xa4\xcf\xb0']


>>> os.stat("d:/预习预习")
nt.stat_result(st_mode=16749, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid
=0, st_size=4096L, st_atime=1515591668L, st_mtime=1515591668L, st_ctime=15148997
47L)
>>> os.stat("d:/ycyc")
nt.stat_result(st_mode=16749, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid
=0, st_size=4096L, st_atime=1515591668L, st_mtime=1515591668L, st_ctime=15155922
05L)
>>>


>>> shutil.copytree("d:/预习预习","d:/ycyc1", "True")
>>> os.stat("d:/ycyc1")
nt.stat_result(st_mode=16749, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid
=0, st_size=4096L, st_atime=1515591668L, st_mtime=1515591668L, st_ctime=15155924
56L)
>>> shutil.copytree("d:/预习预习","d:/ycyc2", "False")
>>> os.stat("d:/ycyc2")
nt.stat_result(st_mode=16749, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid
=0, st_size=4096L, st_atime=1515591668L, st_mtime=1515591668L, st_ctime=15155924
86L)
>>> os.listdir("d:/ycyc1")
['1d', '20180104', '2f', '3f', '4f', 'bb', '\xce\xd2\xb5\xc4os\xd4\xa4\xcf\xb0']


>>> os.listdir("d:/ycyc2")
['1d', '20180104', '2f', '3f', '4f', 'bb', '\xce\xd2\xb5\xc4os\xd4\xa4\xcf\xb0']


>>>


注意如果newdir存在的话,是不可覆盖的:



>>> shutil.copytree("d:/预习预习","d:/ycyc",True)
Traceback (most recent call last):
  File "", line 1, in
  File "D:\Python27\lib\shutil.py", line 191, in copytree
    os.makedirs(dst)
  File "D:\Python27\lib\os.py", line 157, in makedirs
    mkdir(name, mode)
WindowsError: [Error 183] : 'd:/ycyc'
>>>























你可能感兴趣的:(python学习笔记)