zoukankan      html  css  js  c++  java
  • 内置模块:shutil模块,shelve模块,ConfigParser模块,hashlib模块

    shutil模块:
     高级的文件,文件夹,压缩包处理模块:
     
     1. shutil.copyfileobj(fsrc, fdst[,length])
      将文件内容拷贝到另一个文件中,可以部分内容
     
     
     2. shutil.copyfile(src, dst)
      将文件内容拷贝到另一个文件中,可以部分内容
      
     3. shutil.copymode(src, dst)
      仅拷贝权限,内容,组,用户均不变
      
      
     4. shutil.copystat(stc, dst)
      拷贝状态信息,包括:mode bit,atime.mtime,flag
      
     5. shutil.copy(stc,dst)
      拷贝文件和权限
      
     6. shutil.copy2(src, dst)
      拷贝文件和属性
      
      
    shelve模块:
     是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式;
     
     例1:
     
     import shelve
     
     d = shelve.open("shelve.test")     #打开一个文件
     class Test(Object):
      def __init__(self, n):
      self.n =n
      
     t = Test(123)
     t2 = Test(12345)
     
     name = ["brace","kitty","lucy"]
     
     d["test"] = name
     d["t1"] = t
     d["t2"] = t2
     
     d.close()
     
     例2;
     序列化:
      import shelve
      d = shelve.open("shelve.test")
      name = ["brace", "kitty", "lucy"]
      d["name"] = name
      
      执行后会产生3个文件:
      shelve.test.bak
      shelve.test.date
      shelve.test.dir
      
      可以不用理会
      
     反序列化: 
      x = d.get("name")
      print(x)
     
      ['brace', 'kitty', 'lucy']
     
     
    ConfigParser模块:
     
     生成conf文件:
      import configparser
      
      config = configparser.ConfigParser()
      
      config["DEFAULT"] = {'ServerAliveInterval': '45',
           'Compression': 'yes',
           'CompressionLevel': '9'}
      
      config['bitbucket.org'] = {}
      config['bitbucket.org']['User'] = 'hg'
      
      config['topsecret.server.com'] = {}
      config['topsecret.server.com']['Host Port'] = '50022'  # mutates the parser
      config['topsecret.server.com']['ForwardX11'] = 'no'  # same here
      
      config['DEFAULT']['ForwardX11'] = 'yes'
      
      
      with open('test.conf', 'w') as configfile:
       config.write(configfile)
       
     
     读取conf文件:
     
     import configparser
     
     conf = configparser.ConfigParser()
     
     conf.read("test.conf")
     print(conf.defaults())
     print(conf.sections())
     print(conf["topsecret.server.com"]['Port'])
     
     输出:
     OrderedDict([('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes')])
     ['bitbucket.org', 'topsecret.server.com']
     50022
     
     
    hashlib模块:
     用于加密相关的操作,3.x中代替了md5模块和sha模块,主要提供sha224,sha256,sha384,sha512,md5算法;
     
     import hashlib
     m = hashlib.md5()
     m.update(b"this is brace!")
     print(m.hexdigest())
     m.update(b"It is me ")
     print(m.hexdigest())      #十六进制的
     m.update(b"Hello it is me ")
     print(m.hexdigest())
     输出:
     0a4de653ba7a58a3d33311fac5863eb2   #b"this is brace!"
     6bdd392fd9ff3457166823ba2fbe4f8b   #b"this is brace!" + b"It is me "
     bfdc7779e0e7ab776078fb52ba75d6ec   #b"this is brace!" + b"It is me " + b"Hello it is me "
     
     
     n = hashlib.md5()
     n.update(b"this is brace!It is me Hello it is me ")
     print(n.hexdigest())
     输出:
     bfdc7779e0e7ab776078fb52ba75d6ec
     
     
     
     hmac速度快:
     import hmac
     h = hmac.new(b"key:ssss", "value:ssssss你好".encode(encoding = "utf-8"))
     print(h.digest())
     print(h.hexdigest())
  • 相关阅读:
    WPF 中依赖属性的继承(Inherits)
    使用FluentValidation来进行数据有效性验证
    使用ASP.Net WebAPI构建REST服务(六)——Self-Host
    使用ASP.Net WebAPI构建REST服务(五)——客户端
    使用ASP.Net WebAPI构建REST服务(四)——参数绑定
    使用ASP.Net WebAPI构建REST服务(三)——返回值
    使用ASP.Net WebAPI构建REST服务(二)——路由
    使用ASP.Net WebAPI构建REST服务(一)——简单的示例
    WPF在代码中创建DataTemplate时候的异常
    一个简单的WeakList的实现
  • 原文地址:https://www.cnblogs.com/brace2011/p/9226373.html
Copyright © 2011-2022 走看看