• python模块--os&sys&shutil


    os 模块

    提供对操作系统进行调用的接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
    os.chdir("dirname")  改变当前脚本工作目录;相当于shell下cd
    os.curdir  返回当前目录: ('.')
    os.pardir  获取当前目录的父目录字符串名:('..')
    os.makedirs('dirname1/dirname2')    可生成多层递归目录
    os.removedirs('dirname1')    若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
    os.mkdir('dirname')    生成单级目录;相当于shell中mkdir dirname
    os.rmdir('dirname')    删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
    os.listdir('dirname')    列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
    os.remove()  删除一个文件
    os.rename("oldname","newname")  重命名文件/目录
    os.stat('path/filename')  获取文件/目录信息
    os.sep    输出操作系统特定的路径分隔符,win下为"\",Linux下为"/"
    os.linesep    输出当前平台使用的行终止符,win下为" ",Linux下为" "
    os.pathsep    输出用于分割文件路径的字符串
    os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
    os.system("bash command")  运行shell命令,直接显示
    os.environ  获取系统环境变量
    os.path.abspath(path)  返回path规范化的绝对路径
    os.path.split(path)  将path分割成目录和文件名二元组返回
    os.path.dirname(path)  返回path的目录。其实就是os.path.split(path)的第一个元素
    os.path.basename(path)  返回path最后的文件名。如何path以/或结尾,那么就会返回空值。即os.path.split(path)的第二个元素
    os.path.exists(path)  如果path存在,返回True;如果path不存在,返回False
    os.path.isabs(path)  如果path是绝对路径,返回True
    os.path.isfile(path)  如果path是一个存在的文件,返回True。否则返回False
    os.path.isdir(path)  如果path是一个存在的目录,则返回True。否则返回False
    os.path.join(path1[, path2[, ...]])  将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
    os.path.getatime(path)  返回path所指向的文件或者目录的最后存取时间
    os.path.getmtime(path)  返回path所指向的文件或者目录的最后修改时间

    更多猛击这里 

    实际操作

     1 >>> import os
     2 >>> a = os.system("dir")
     3  驱动器 D 中的卷没有标签。
     4  卷的序列号是 000C-ED63
     5 
     6  D:Python35-32 的目录
     7 
     8 2016/01/24  00:59    <DIR>          .
     9 2016/01/24  00:59    <DIR>          ..
    10 2016/01/24  00:59    <DIR>          DLLs
    11 2016/01/24  00:58    <DIR>          Doc
    12 2016/01/24  00:58    <DIR>          include
    13 2016/01/24  00:59    <DIR>          Lib
    14 2016/01/24  00:59    <DIR>          libs
    15 2015/12/07  19:56            30,338 LICENSE.txt
    16 2015/12/06  06:32           310,468 NEWS.txt
    17 2015/12/06  01:40            38,680 python.exe
    18 2015/12/06  01:39            51,480 python3.dll
    19 2015/12/06  01:39         3,122,968 python35.dll
    20 2015/12/06  01:40            38,680 pythonw.exe
    21 2015/11/22  22:58             8,269 README.txt
    22 2016/02/24  20:25    <DIR>          Scripts
    23 2016/01/24  00:59    <DIR>          tcl
    24 2016/01/24  00:59    <DIR>          Tools
    25 2015/06/25  23:34            85,328 vcruntime140.dll
    26                8 个文件      3,686,211 字节
    27               10 个目录 193,337,020,416 可用字节
    28 >>> a
    29 0
    30 >>> b = os.popen("dir")
    31 >>> b
    32 <os._wrap_close object at 0x002FA9F0>
    33 >>> print(b)
    34 <os._wrap_close object at 0x002FA9F0>
    35 >>> b = os.popen("dir").read()
    36 >>> b
    37 ' 驱动器 D 中的卷没有标签。
     卷的序列号是 000C-ED63
    
     D:\Python35-32 的目录
    38 n
    2016/01/24  00:59    <DIR>          .
    2016/01/24  00:59    <DIR>          ..
    39 
    2016/01/24  00:59    <DIR>          DLLs
    2016/01/24  00:58    <DIR>
    40 Doc
    2016/01/24  00:58    <DIR>          include
    2016/01/24  00:59    <DIR>
    41       Lib
    2016/01/24  00:59    <DIR>          libs
    2015/12/07  19:56
    42   30,338 LICENSE.txt
    2015/12/06  06:32           310,468 NEWS.txt
    2015/12/06
    43 01:40            38,680 python.exe
    2015/12/06  01:39            51,480 python3.
    44 dll
    2015/12/06  01:39         3,122,968 python35.dll
    2015/12/06  01:40
    45     38,680 pythonw.exe
    2015/11/22  22:58             8,269 README.txt
    2016/02/
    46 24  20:25    <DIR>          Scripts
    2016/01/24  00:59    <DIR>          tcl
    20
    47 16/01/24  00:59    <DIR>          Tools
    2015/06/25  23:34            85,328 vcr
    48 untime140.dll
                   8 个文件      3,686,211 字节
                  1049 目录 193,337,020,416 可用字节
    '
    50 >>> print(b)
    51  驱动器 D 中的卷没有标签。
    52  卷的序列号是 000C-ED63
    53 
    54  D:Python35-32 的目录
    55 
    56 2016/01/24  00:59    <DIR>          .
    57 2016/01/24  00:59    <DIR>          ..
    58 2016/01/24  00:59    <DIR>          DLLs
    59 2016/01/24  00:58    <DIR>          Doc
    60 2016/01/24  00:58    <DIR>          include
    61 2016/01/24  00:59    <DIR>          Lib
    62 2016/01/24  00:59    <DIR>          libs
    63 2015/12/07  19:56            30,338 LICENSE.txt
    64 2015/12/06  06:32           310,468 NEWS.txt
    65 2015/12/06  01:40            38,680 python.exe
    66 2015/12/06  01:39            51,480 python3.dll
    67 2015/12/06  01:39         3,122,968 python35.dll
    68 2015/12/06  01:40            38,680 pythonw.exe
    69 2015/11/22  22:58             8,269 README.txt
    70 2016/02/24  20:25    <DIR>          Scripts
    71 2016/01/24  00:59    <DIR>          tcl
    72 2016/01/24  00:59    <DIR>          Tools
    73 2015/06/25  23:34            85,328 vcruntime140.dll
    74                8 个文件      3,686,211 字节
    75               10 个目录 193,337,020,416 可用字节
    View Code

    sys模块

    1
    2
    3
    4
    5
    6
    7
    8
    sys.argv           命令行参数List,第一个元素是程序本身路径
    sys.exit(n)        退出程序,正常退出时exit(0)
    sys.version        获取Python解释程序的版本信息
    sys.maxint         最大的Int
    sys.path           返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
    sys.platform       返回操作系统平台名称
    sys.stdout.write('please:')
    val = sys.stdin.readline()[:-1]

    shutil

    高级的 文件、文件夹、压缩包 处理模块

    shutil.copyfileobj(fsrc, fdst[, length])
    将文件内容拷贝到另一个文件中,可以部分内容

     View Code

    shutil.copyfile(src, dst)
    拷贝文件

     View Code

    shutil.copymode(src, dst)
    仅拷贝权限。内容、组、用户均不变

     View Code

    shutil.copystat(src, dst)
    拷贝状态的信息,包括:mode bits, atime, mtime, flags

     View Code

    shutil.copy(src, dst)
    拷贝文件和权限

     View Code

    shutil.copy2(src, dst)
    拷贝文件和状态信息

     View Code

    shutil.ignore_patterns(*patterns)
    shutil.copytree(src, dst, symlinks=False, ignore=None)
    递归的去拷贝文件

    例如:copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))

     View Code

    shutil.rmtree(path[, ignore_errors[, onerror]])
    递归的去删除文件

     View Code

    shutil.move(src, dst)
    递归的去移动文件

     View Code

    shutil.make_archive(base_name, format,...)

    创建压缩包并返回文件路径,例如:zip、tar

    • base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
      如:www                        =>保存至当前路径
      如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
    • format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
    • root_dir: 要压缩的文件夹路径(默认当前目录)
    • owner: 用户,默认当前用户
    • group: 组,默认当前组
    • logger: 用于记录日志,通常是logging.Logger对象
    1
    2
    3
    4
    5
    6
    7
    8
    9
    #将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录
     
    import shutil
    ret = shutil.make_archive("wwwwwwwwww"'gztar', root_dir='/Users/wupeiqi/Downloads/test')
    ###wwwwwwww是压缩后的文件名, gztar 是压缩后的文件后缀为*.tar.gz 
    ###目录名在windows下的写法需要转义。例如OSError: [WinError 123] 文件名、目录名或卷标语法不正确。: 'e: est eadmetest'
    ###同时在pycharm中会在:后的第一个字母有突出提示
     
    #将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录
    import shutil
    ret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww"'gztar', root_dir='/Users/wupeiqi/Downloads/test')
    ###/Users/wupeiqi/wwwwwwwww 路径如果不存在,会自动新建
     View Code

    shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:

     zipfile 压缩解压
     tarfile 压缩解压
     ZipFile
     TarFile 
  • 相关阅读:
    (剑指Offer)------二进制中1的个数
    LeetCode#58:最后一个单词的长度解析
    js 生成四个随机字母或数字+js获取当前日期
    ES6学习笔记----数组的扩展
    No component factory found for ListenerAddComponent. Did you add it to @NgModule.entryComponents?
    Can't bind to 'formGroup' since it isn't a known property of 'form'
    算法初相识---插入排序,冒泡排序,选择排序,以及分析算法
    Deno MongoDB 增删查改 接口
    Deno MySQL 增删查改接口
    Deno 几种常用的传参方式
  • 原文地址:https://www.cnblogs.com/qing-add/p/5224972.html
Copyright © 2020-2023  润新知