• python 常用模块


    介绍下python的常用模块:

    目录:

    1.time

    2.shutil

    3.zipfile & tarfile

    4.json & pickle

    5.xml & configparser

    6.hashlib 

    7.subprocess

    8.logging

    第一部分:time

    python中关于时间的模块 time.

    python中的时间分三类, 第一类:unix时间戳类型 time.time()就会将当前的时间换算程unix时间戳打印出来.第二类是结构化的时间,第三类是格式化后的时间字符串.下面分别举例字:

     1 In [21]: import time
     2 
     3 In [22]: time.time()   #时间戳)
     4 Out[22]: 1497031848.168647
     5   
     6 In [23]: time.strftime('%Y-%m-%d %X')       #格式化后的时间字符串
     7 Out[23]: '2017-06-10 02:11:03'
     8 
     9 In [26]: time.localtime()                                #格式化时间
    10 Out[26]: time.struct_time(tm_year=2017, tm_mon=6, tm_mday=10, tm_hour=2, tm_min=11, tm_sec=42, tm_wday=5, tm_yday=161, tm_isdst=0)
    View Code

    time模块常用的是格式化为字符串的时间.有时也用到time.ctime() 函数

    time模块中还有一个重要函数是sleep() 这个函数的作用是让当前进程休眠,sleep(seconds)函数接收一个整数,表示休眠多少秒,比如我们想让程序休眠5s,则使用time.sleep(5)来完成.

    第二部分:shutil

    shutil是python中高级的文件/文件夹/压缩包处理模块,它有着比os模块操作文件/目录/压缩包方面更优异的表现.下面举例字:

     1 #复制目录树
     2 In [30]: import shutil
     3 
     4 In [31]: dir
     5 Out[31]: <function dir>
     6 
     7 In [32]: ls
     8 getgoods.py  gouwuche.zip  readme.txt
     9 gouwuche/    new.json
    10 
    11 In [33]: shutil.copytree('gouwuche','gouwuche.test')
    12 
    13 In [34]: ls
    14 getgoods.py  gouwuche.test/  new.json
    15 gouwuche/    gouwuche.zip    readme.txt
    16 
    17 
    18 #拷贝文件和权限
    19 In [35]: shutil.copy('readme.txt','readyou.txt')
    20 
    21 In [36]: ls
    22 getgoods.py  gouwuche.test/  new.json    readyou.txt
    23 gouwuche/    gouwuche.zip    readme.txt
    24 
    25 #递归的删除目录
    26 
    27 In [37]: shutil.rmtree('gouwuche.test')
    28 
    29 In [38]: ls
    30 getgoods.py  gouwuche.zip  readme.txt
    31 gouwuche/    new.json      readyou.txt
    View Code

    第三部分:zipfile & tarfile

    (1).zipfile

          zipefile 用于 使用文件压缩算法zip 操作文件的一个模块.

           创建一个zip文件,并向其中添加文件

    1 1 In [69]: import zipfile
    2 2 
    3 3 In [70]: f=zipfile.ZipFile('test.zip','a')
    4 4 
    5 5 In [71]: f.write('s17day01.txt')
    6 6 
    7 7 In [72]: f.write('username.txt')
    8 8 
    9 9 In [73]: f.close()
    View Code

           查看zip文件内容

    1 1 In [10]: l=zipfile.ZipFile('test.zip','r')
    2 2 
    3 3 In [19]: l.namelist()
    4 4 Out[19]: ['s17day01.txt', 'username.txt']
    View Code

          解压zip文件,可以从下面的结果中看到,解压成功了.

     1 In [20]: rm s17day01.txt
     2 
     3 In [21]: rm username.txt
     4 
     5 In [22]: ls
     6 test.zip
     7 
     8 In [23]: l
     9 Out[23]: <zipfile.ZipFile at 0x7f5bc457a5d0>
    10 
    11 In [24]: l.extractall()
    12 
    13 In [25]: ls
    14 s17day01.txt  test.zip  username.txt
    View Code

            综合联系,1.我们创建了一个加密吗的zip文件,之后我们用zipfile暴破一下zip文件.

     1 localhost:~/0615$ ll
     2 总用量 12
     3 drwxr-xr-x  3 notu notu 4096 6月   9 12:51 ./
     4 drwxr-xr-x 78 notu notu 4096 6月   9 12:43 ../
     5 drwxr-xr-x  2 notu notu 4096 6月   9 12:50 test/
     6 localhost:~/0615$ zip test.zip -e -r test/*
     7 Enter password:                                      #输入123@123
     8 Verify password:                                    #输入123@123
     9   adding: test/s17day01.txt (stored 0%)
    10   adding: test/username.txt (stored 0%)
    View Code

            2.接着,我们创建一个密码文件:

     1 #password.txt 文件内容
     2 123456
     3 654321
     4 dajfk
     5 aifjei
     6 vlcjfkd
     7 akejfk
     8 kejre
     9 a
    10 123@123
    11 kdfjke
    12 kjfejfio
    13 kajfoad
    14 kj
    View Code
     1 #!/usr/bin/env python
     2 #-*- coding:utf-8 -*-
     3 
     4 import zipfile
     5 """根据字典文件,暴力破解zip密码"""
     6 
     7 def extract_zip(zfile,password):
     8     try:
     9         zfile.extractall(pwd=password)
    10         print('password found:%s' %password)
    11         return password
    12     except:
    13         pass
    14 
    15 def main():
    16     zfile=zipfile.ZipFile('test.zip')
    17     with open('password.txt','r') as f:
    18         for password in f:
    19             pw=extract_zip(zfile,password.strip().encode('utf-8'))
    20             if pw:
    21                 print('Password=%s' %pw.decode('utf-8'))
    22                 break
    23         else:
    24             print('not found')
    25     zfile.close()
    26 
    27 
    28 if __name__=='__main__':
    29     main()
    View Code

    执行结果:

    1 localhost:~/0615$ python3.5 decry.py 
    2 password found:b'123@123'
    3 Password=b'123@123'

    (2) tarfile

    我们同样使用例子来学习tarfile的日常用法.

        创建一个tar包,并且向其中添加文件.

     1 ln[6]: import tarfile
     2 
     3 In [7]: ls
     4 decry.py  password.txt  test/  test.zip
     5 
     6 In [8]: t_f=tarfile.TarFile('0615.tar','a')
     7 
     8 In [9]: dir(t_f)
     9 
    10 In [12]: t_f.add('test')
    11 
    12 In [13]: t_f.add('decry.py')
    13 
    14 In [14]: t_f.add('password.txt')
    15 
    16 In [15]: t_f.close()
    View Code

        查看tar包的内容.

     1 import tarfile
     2 
     3 tf=tarfile.TarFile('0615.tar')
     4 tf.list()
     5 tf.close()
     6 
     7 #显示结果
     8 /usr/bin/python3.5 /home/notu/0615/tar_t.py
     9 ?rwxr-xr-x notu/notu          0 2017-06-09 13:22:19 test/ 
    10 ?rw-r--r-- notu/notu          0 2017-06-09 13:23:54 test/username.txt 
    11 ?rw-r--r-- notu/notu          0 2017-06-09 13:23:54 test/s17day01.txt 
    12 ?rw-r--r-- notu/notu        622 2017-06-09 13:23:51 decry.py 
    13 ?rw-r--r-- notu/notu         85 2017-06-09 13:02:42 password.txt 
    View Code

      解包

    1 import tarfile
    2 
    3 tf=tarfile.TarFile('0615.tar')
    4 tf.extractall(path='tartest')
    5 tf.close()
    6 
    7 
    8 #结果
    9 在当前目录创建了一个tartest目录,tar包的内容被添加到tartest目录中
    View Code

    第四部分:json & pickle

    json,一种数据编码格式,常用来作为数据对象在不同编程语言间传递.之前的xml也有这个功能,但是json较xml快,且使用更加方便.下面举个例子:

     1 #json存储变量
     2 In [39]: d={'1':111}
     3 In [42]: json.dumps(d)
     4 Out[42]: '{"1": 111}'
     5 
     6 In [43]: b=json.dumps(d)
     7 
     8 In [44]: c=json.loads(b)
     9 
    10 In [45]: c
    11 Out[45]: {u'1': 111}
    12 
    13 
    14 #json 将变量存储到文件中
    15 In [46]: with open('a.json','w') as f:
    16     ...:     json.dump(b,f)
    17     ...:     
    18 
    19 
    20 #json将变量从文件中取出
    21 
    22 In [47]: with open('a.json','r') as f:
    23     ...:     d=json.load(f)
    24     ...:     
    25 
    26 In [48]: d
    27 Out[48]: u'{"1": 111}'
    View Code

    pickle常用来序列化对象,比如我们程序中使用的某个对象可以通过pickle直接存储到文件中,将来可以通过pickle直接从文件中获取,这样省去了重新生成对象的麻烦,下面举个例子:

     1 In [50]: import pickle
     2 #将序列化的对象存到变量中.
     3 
     4 In [51]: d={'name':'xiaoming','age':23,'sex':'mail'}
     5 
     6 In [52]: print(type(d))
     7 <type 'dict'>
     8 
     9 In [53]: h=pickle.dumps(d)
    10 
    11 In [55]: print(type(h))
    12 <type 'str'>
    13 
    14 #将序列化的数据存放到文件中
    15 In [56]: with open('test.pickle','w') as f:
    16     ...:     pickle.dump(h,f)
    17     ...:     
    18 
    19 #从文件中取出pickle对象
    20 In [57]: with open('test.pickle','r') as f:
    21     ...:     e=pickle.load(f)
    22     ...:     
    23 
    24 In [58]: e
    25 Out[58]: "(dp0
    S'age'
    p1
    I23
    sS'name'
    p2
    S'xiaoming'
    p3
    sS'sex'
    p4
    S'mail'
    p5
    s."
    26 
    27 In [59]: print(type(e))
    28 <type 'str'>
    29 
    30 #将数据从pickle数据结构中还原
    31 In [60]: pickle.loads(e)
    32 Out[60]: {'age': 23, 'name': 'xiaoming', 'sex': 'mail'}
    View Code

    第五部分:xml & configparser

    xml也是不同编程语言间传递数据的一种协议,和json类似.下面是一个简单的xml例子,其中它有对称的<>括起来的数据结构

    1 <note>
    2 <to>George</to>
    3 <from>John</from>
    4 <heading>Reminder</heading>
    5 <body>Don't forget the meeting!</body>
    6 </note>
    View Code

    python中可以通过 "xml.etree.ElementTree" 来操作xml文件. 下面举一个例子:

     1 import xml.etree.ElementTree as ET
     2  
     3 tree = ET.parse("xmltest.xml")
     4 root = tree.getroot()
     5 print(root.tag)
     6  
     7 #遍历xml文档
     8 for child in root:
     9     print('========>',child.tag,child.attrib,child.attrib['name'])
    10     for i in child:
    11         print(i.tag,i.attrib,i.text)
    12  
    13 #只遍历year 节点
    14 for node in root.iter('year'):
    15     print(node.tag,node.text)
    16 #---------------------------------------
    17 
    18 import xml.etree.ElementTree as ET
    19  
    20 tree = ET.parse("xmltest.xml")
    21 root = tree.getroot()
    22  
    23 #修改
    24 for node in root.iter('year'):
    25     new_year=int(node.text)+1
    26     node.text=str(new_year)
    27     node.set('updated','yes')
    28     node.set('version','1.0')
    29 tree.write('test.xml')
    30  
    31  
    32 #删除node
    33 for country in root.findall('country'):
    34    rank = int(country.find('rank').text)
    35    if rank > 50:
    36      root.remove(country)
    37  
    38 tree.write('output.xml')
    39 
    40 
    41 
    42 #在country内添加(append)节点year2
    43 import xml.etree.ElementTree as ET
    44 tree = ET.parse("a.xml")
    45 root=tree.getroot()
    46 for country in root.findall('country'):
    47     for year in country.findall('year'):
    48         if int(year.text) > 2000:
    49             year2=ET.Element('year2')
    50             year2.text='新年'
    51             year2.attrib={'update':'yes'}
    52             country.append(year2) #往country节点下添加子节点
    53 
    54 tree.write('a.xml.swap')
    View Code

    linux 或windows系统中现在还保留很多ini 格式的文件.它们的样子类似这样:

    1 [General]
    2 StartWithLastProfile=1
    3 
    4 [Profile0]
    5 Name=default
    6 IsRelative=1
    7 Path=5rc9bfaq.default
    8 Default=1

    我们可以通过configparser 模块解析这种ini文件内容.下面看一个例子:

     1 >>> import configparser
     2 >>> config=configparser.ConfigParser()
     3 >>> config.read('test.ini')
     4 ['test.ini']
     5 
     6 #查看所有标题:
     7 >>> config.sections()
     8 ['General', 'Profile0']
     9 
    10 #查看某个section中的所有键
    11 >>> config.options('General')
    12 ['startwithlastprofile']
    13 
    14 #显示键值对
    15 >>> config.items('General')
    16 [('startwithlastprofile', '1')]
    17 
    18 #获取某个键的值
    19 >>> config.options('Profile0')
    20 ['name', 'isrelative', 'path', 'default']
    21 >>> config.get('Profile0','path')
    22 '5rc9bfaq.default'
    23 
    24 #获取整数
    25 >>> config.getint('Profile0','default')
    26 1
    View Code

    第六部分:hashlib

    hashlib模块中包含了很多hash算法的函数,我们平时可以用hashlib模块计算某些文件或字符串的哈希值.

    md5的例子:

     1 In [24]: ls
     2 getgoods.py  gouwuche/  gouwuche.zip  new.json  readme.txt
     3 
     4 In [25]: import hashlib
     5 
     6 In [26]: m=hashlib.md5()                   #获取md5值
     7 
     8 In [27]: m.update('gouwuche.zip')
     9 
    10 In [28]: m.hexdigest()
    11 Out[28]: '61d08dc2aa2f5c4a7f1883aaebacb63b'
    View Code

    计算一个普通文本文件的md5值:

     1 In [8]: ls
     2 getgoods.py  gouwuche/  gouwuche.zip  new.json  readme.txt
     3 
     4 In [9]: import hashlib
     5 
     6 In [10]: m=hashlib.md5()
     7 
     8 In [13]: with open('new.json','r') as f:
     9     ...:     for line in f:
    10     ...:         m.update(line)
    11     ...:         
    12     ...:     
    13 
    14 In [14]: m.hexdigest()
    15 Out[14]: '4e09be5b2afe24d13ac9afa9e70a4041'
    16 
    17 我们使用linux系统的md5sum 计算'new.json"的值:
    18 /zuoye$ md5sum new.json 
    19 4e09be5b2afe24d13ac9afa9e70a4041  new.json
    20 
    21 相同的
    View Code

    第七部分:subprocess

    python中的subprocess模块可以执行系统的命令,且可以模拟linux系统中的管道操作.subprocess功能强大,下面从例子中学习subprocess模块

    subprocess可以使用call()和Popen() 执行系统命令,两个方法的区别是call返回命令的执行的退出码,比如在linux系统中,退出码为"0"表示命令正常结束,非"0"说明,脚本执行有问题.而"Popen"默认返回的是命令的执行结果.如下:

     1 In [1]: import subprocess
     2 
     3 In [2]: cmd='ls -l'
     4 
     5 In [4]: cd zuoye
     6 /home/notu/zuoye
     7 
     8 In [5]: res=subprocess.call(cmd,shell=True)
     9 
    10 
    11 In [6]: res
    12 Out[6]: 0
    13 
    14 可以看到返回值为0,说明命令执行成功
    15 
    16 In [1]: import subprocess
    17 
    18 In [2]: cmd='ls /tmp'
    19 
    20 In [3]: res=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
    21 
    22 In [4]:a= res.stdout.read().split('
    ')
    23 
    24 ln[5]:a
    25 Out[14]: 
    26 ['config-err-GKVYcT',
    27  'fcitx-socket-:0',
    28  'firefox_notu',
    29  'hsperfdata_notu',
    30  'plugtmp',
    31  'snap.0_wifi-ap_AeEGqS',
    32  'ssh-N7h63RaQ4sAy',
    33  'systemd-private-87c00976bf554756b13ed0f5b9cc7d8c-colord.service-U5VPAo',
    34  'systemd-private-87c00976bf554756b13ed0f5b9cc7d8c-fwupd.service-8KDw74',
    35  'systemd-private-87c00976bf554756b13ed0f5b9cc7d8c-rtkit-daemon.service-wwUFsF',
    36  'systemd-private-87c00976bf554756b13ed0f5b9cc7d8c-systemd-resolved.service-ETg8oa',
    37  'systemd-private-87c00976bf554756b13ed0f5b9cc7d8c-systemd-timesyncd.service-QYCI86',
    38  'tmp86bep6xh',
    39  'unity_support_test.0',
    40  '']
    View Code

        上面的例子中可以看到call和Poen命令的区别.

    下面我们建立一个subprocess工厂:

     1 In [15]: def multi(*args):
     2     ...:     for cmd in args:
     3     ...:         p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
     4     ...:         out=p.stdout.read().split('
    ')
     5     ...:         print(out)
     6     ...:    
     7 
     8 In [5]: for i in multi('uptime','free -m','date'):
     9    ...:     print(i)
    10    ...:     
    11 [' 01:40:59 up  4:47,  1 user,  load average: 0.21, 0.26, 0.33', '']
    12 ['              total        used        free      shared  buff/cache   available', 'Mem:          15617        1593       11368         338        2655       13367', 'Swap:         15946           0       15946', '']
    13 ['2017xe5xb9xb4 06xe6x9cx88 10xe6x97xa5 xe6x98x9fxe6x9cx9fxe5x85xad 01:40:59 CST', '']
    View Code

    第八部分:logging

    python中标准的记录日志的模块logger

    一:如果不指定filename,则默认打印到终端
    二:指定日志级别:
        指定方式:
            1:level=10
            2:level=logging.ERROR
    
        日志级别种类:
            CRITICAL = 50
            FATAL = CRITICAL
            ERROR = 40
            WARNING = 30
            WARN = WARNING
            INFO = 20
            DEBUG = 10
            NOTSET = 0

     下面看一个例子:

     1 logging.basicConfig(filename='access.log',
     2                     format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
     3                     datefmt='%Y-%m-%d %H:%M:%S %p',
     4                     level=10)
     5 
     6 logging.debug('debug')
     7 logging.info('info')
     8 logging.warning('warning')
     9 logging.error('error')
    10 logging.critical('critical')
    11 logging.log(10,'log') #如果level=40,则只有logging.critical和loggin.error的日志会被打印
    View Code
  • 相关阅读:
    〖編程·C++〗回溯算法:排列树 N皇后问题
    〖網頁·PHP〗Windows 7下进行PHP环境搭建
    〖編程·C++〗回溯算法:排列树 工作分配问题
    〖編程·C++〗回溯算法:完全N叉树 最佳调度问题 以及相关思考
    pod install 初始化失败 RuntimeError [Xcodeproj] Unknown object version.
    iOS 版本判定
    Windows部署多版本Mysql服务
    注意IE的Cookie个数限制问题
    检索 COM 类工厂中的组件时失败,原因是出现以下错误: 8007007e
    一次曲折的单点集成之旅
  • 原文地址:https://www.cnblogs.com/mingxiazhichan/p/6970266.html
Copyright © 2020-2023  润新知