zoukankan      html  css  js  c++  java
  • shutil模块和os模块对比

    一、shutil -- 是一种高层次的文件操作工具类似于高级API,而且主要强大之处在于其对文件的复制与删除操作更是比较支持好。

    1、shutil.copy(src,dst)复制一个文件到另一个目录下,返回dst路径。dst可以是一个文件,或者是一个目录。但src必须是一个文件,否则会报错。

    >>> shutil.copy("e:\test\pp.txt","f:\yy.txt")
    'f:\yy.txt'
    >>> shutil.copy("e:\test","f:\")
    Traceback (most recent call last):
    File "<pyshell#15>", line 1, in <module>
    shutil.copy("e:\test","f:\")
    File "D:Program Filespython3.6libshutil.py", line 235, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
    File "D:Program Filespython3.6libshutil.py", line 114, in copyfile
    with open(src, 'rb') as fsrc:
    PermissionError: [Errno 13] Permission denied: 'e:\test'

    2、shutil.copy2(src,dst)在copy上的基础上再复制文件最后访问时间与修改时间也复制过来了

    3、shutil.copyfile(src,dst)从源src复制到dst中去,src和dst必须是文件,不能是文件夹

    >>> shutil.copyfile("e:\test\pp.txt","f:\oo.txt")
    'f:\oo.txt'
    >>> shutil.copyfile("e:\test\pp.txt","f:\")
    Traceback (most recent call last):
      File "<pyshell#19>", line 1, in <module>
        shutil.copyfile("e:\test\pp.txt","f:\")
      File "D:Program Filespython3.6libshutil.py", line 115, in copyfile
        with open(dst, 'wb') as fdst:
    FileNotFoundError: [Errno 2] No such file or directory: 'f:\'

    4、shutil.copystat( src, dst)只复制权限、最后访问时间、最后修改时间,不会复制文件内容

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

    >>> shutil.copytree("e:\test\","f:\yy")
    'f:\yy'

    6、shutil.copymode(src,dst)只复制权限

    7、shutil.move(src,dst)将路径src处的文件夹移动到路径dst,并返回新位置的绝对路径字符串。src可以是一个文件夹,也可以是一个文件。

    如果dst目录下已经存在了src中的文件名,则会报错。

    >>> shutil.move("e:\test\pp.txt","f:\yy\uu")
    'f:\yy\uu'
    >>> shutil.move("e:\test","f:\")
    'f:\test'
    >>> shutil.move("e:\answer1.txt","f:\yy")
    Traceback (most recent call last):
      File "<pyshell#27>", line 1, in <module>
        shutil.move("e:\answer1.txt","f:\yy")
      File "D:Program Filespython3.6libshutil.py", line 536, in move
        raise Error("Destination path '%s' already exists" % real_dst)
    shutil.Error: Destination path 'f:yyanswer1.txt' already exists

    这里注意一点:如果src中的文件只有只读权限,则会报错,但src文件仍然正常的copy到了dst下。但src不会被删除

    >>> shutil.move("e:\answer1.txt","f:\yy")
    Traceback (most recent call last):
      File "D:Program Filespython3.6libshutil.py", line 538, in move
        os.rename(src, real_dst)
    OSError: [WinError 17] 系统无法将文件移到不同的磁盘驱动器。: 'e:\answer1.txt' -> 'f:\yy\answer1.txt'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<pyshell#28>", line 1, in <module>
        shutil.move("e:\answer1.txt","f:\yy")
      File "D:Program Filespython3.6libshutil.py", line 553, in move
        os.unlink(src)
    PermissionError: [WinError 5] 拒绝访问。: 'e:\answer1.txt'

    如果dst所指的目录不存在,而src是一个文件,所以程序默认会认为dst是指的一个没有扩展名的文件,而不是一个文件夹。

    >>> shutil.move("e:\answer2.txt","f:\qq")
    'f:\qq'

    这样就在f盘下生成了一个名为qq的文件,,如图所示:

    二、os和shutil模块删除文件和文件夹

    1、os.unlink(path)将删除path处的文件,成功删除后没有任何返回。path是一个文件的完整路径

    os.unlink("f:\yy\answer1.txt")

    2、os.rmdir(path)将删除path处的文件夹,该文件夹必须为空。其中没有任何文件和文件夹

    >>> os.rmdir("f:\yy\")
    Traceback (most recent call last):
      File "<pyshell#39>", line 1, in <module>
        os.rmdir("f:\yy\")
    OSError: [WinError 145] 目录不是空的。: 'f:\yy\'
    >>> os.rmdir("f:\yy\test")

    3、shutil.rmtree(path)将删除path处的文件夹,它包含的所有文件和文件夹都将被删除

  • 相关阅读:
    17. Letter Combinations of a Phone Number
    16. 3Sum Closest
    15. 3Sum
    14. Longest Common Prefix
    13. Roman to Integer
    12. Integer to Roman
    11. Container With Most Water
    10. Regular Expression Matching
    9. Palindrome Number
    8. String to Integer (atoi)
  • 原文地址:https://www.cnblogs.com/Lival/p/6389052.html
Copyright © 2011-2022 走看看