zoukankan      html  css  js  c++  java
  • python------模块定义、导入、优化 ------->sys模块,shutil模块

    1.sys模块

    import sys


    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]

    2.shutil模块
    
    

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

    
    

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

                

     
    举例:
    import shutil #把yesterday中的内容复制到yesterday2中 f1 = open("yesterday.txt") f2 = open("yesterday2.txt","w") shutil.copyfileobj(f1,f2)

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

      

    1 import shutil
    2 #把yesterday2拷贝一下
    3 shutil.copyfile("yesterday2.txt","yesterday3.txt")
        
    3)shutil.copymode(src, dst) #仅拷贝权限。(内容、组、用户均不变
    4)shutil.copystat(src, dst) #
    拷贝状态的信息,包括:mode bits, atime, mtime, flags
        5)shutil.copy(src, dst)     #拷贝文件和权限
    6)shutil.copy2(src, dst) #拷贝文件和状态信息
        7)shutil.ignore_patterns(*patterns)
    shutil.copytree(src, dst, symlinks=False, ignore=None)
    #递归的去拷贝文件
    8)shutil.rmtree(path[,ignore_errors[,onerror]])
    #递归的去删除文件
    9)shutil.move(scr,dst) #递归的去移动文件
       10)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对象
     
    shutil.make_archive("zzzzzzzz","zip","E:Python exercisePython2.7练习")
    #"zzzzzzzz":压缩包名; "zip":格式; "E:Python exercisePython2.7练习":要压缩的文件路径
    #压缩之后,压缩包会放在和 .py 相同的路径下
    shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的
    import tarfile
    
    # 压缩
    tar = tarfile.open('your.tar','w')
    tar.add('/Users/wupeiqi/PycharmProjects/bbs2.zip', arcname='bbs2.zip')
    tar.add('/Users/wupeiqi/PycharmProjects/cmdb.zip', arcname='cmdb.zip')
    tar.close()
    
    # 解压
    tar = tarfile.open('your.tar','r')
    tar.extractall()  # 可设置解压地址
    tar.close()
    import zipfile
    
    # 压缩
    z = zipfile.ZipFile('laxi.zip', 'w')
    z.write('a.log')
    z.write('data.data')
    z.close()
    
    # 解压
    z = zipfile.ZipFile('laxi.zip', 'r')
    z.extractall()
    z.close()

    感谢:http://www.cnblogs.com/wupeiqi/articles/4963027.html

     






  • 相关阅读:
    hdu 5053 the Sum of Cube
    [LeetCode] Word Pattern
    [LeetCode] Minimum Depth of Binary Tree
    [C++] std::vector
    [LeetCode] Count Binary Substrings
    [LeetCode] Degree of an Array
    [LeetCode] String to Integer (atoi)
    [LintCode] 比较字符串
    [LeetCode] Valid Parentheses
    [LeetCode] Perfect Number
  • 原文地址:https://www.cnblogs.com/bltstop/p/9588338.html
Copyright © 2011-2022 走看看