• python3-day6(模块)


    一、OS模块

      1、os.system('ls -l') #子shell运行,获取返回值,不是结果。

      2、os.popen('ls -l').read() #获取结果。

     

    二、sys模块

      1、sys.argv

      2、sys.exit

      3、sys.path

    三、shutil模块

      1、压缩归档

    import shutil
    ret = shutil.make_archive("/archive", 'gztar', root_dir='/var/log/www/access.log')
    

      2、zipfile

    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()
    

      3、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()
    

      4、shelve

    import shelve
    def CreateData():
        db=shelve.open('db.dat','c')
        db['int']=1
        db['float']=2.3
        db['string']='i like python.'
        db['key']='value'
        db.close()
    
    def LoadData():
        db=shelve.open('db.dat','r')
        for item in db.items():
            print(item)
        db.close()
    if __name__=="__main__":
        CreateData()
        LoadData()
    

      

     四、subprocess

    import subprocess
    1、call,run
    subprocess.call('df -h',shell=True)
    ret=subprocess.Popen('df -h',shell=True,stdout=subprocess.PIPE)
    print(ret.stdout.read()
    
    2、交互
    obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    obj.stdin.write('print 1 
     ')
    obj.stdin.write('print 2 
     ')
    obj.stdin.write('print 3 
     ')
    obj.stdin.write('print 4 
     ')
    
    out_error_list = obj.communicate()
    print out_error_list
    

      

  • 相关阅读:
    dom event 笔记
    提交安钮 提交一次加了 59秒倒计时
    时间倒计时
    把表单数据封装成json格式 插件可用
    dbgrid显示access备注信息
    stringgird中使用TClientDataSet排序的问题
    【单位矩阵】【杭电OJ1575】
    【矩阵快速幂】【杭电OJ1757】
    【关键路径】【拓扑排序+逆拓扑排序】【转】
    【拓扑排序】【关键路径】
  • 原文地址:https://www.cnblogs.com/weibiao/p/5403089.html
Copyright © 2020-2023  润新知