zoukankan      html  css  js  c++  java
  • 第七章 Python——模块与包

    目录

    一、软件开发目录规范

    二、模块

    三、包

    四、常用模块之——logging模块

    五、常用模块之——re模块

    六、常用模块之——json模块与pickle模块

    七、常用模块之——hashlib模块

    八、常用模块之——time模块与datatime模块

    九、常用模块之——os模块与sys模块

    十、常用模块之——random模块与suprocess模块

    十一、其他模块——shutil模块、shelve模块、xml模块、configparser模块

    一、软件开发目录规范(以ATM+购物车程序为例)

    二、 模块

    什么是模块(what):

    模块是一系列功能的集合体。

    常见的模块形式(自定义模块、第三方模块、内置模块)

    ①一个module.py文件就是一个模块,文件名是module.py,而模块名是module。

    ②一个含有__init__.py文件的文件夹也是模块。

    ③已被编译为共享库或DLL的C或者C++扩展。

    ④使用C语言编写并连接到Python解释器的内置模块。

    为什么要用模块(why):

    ①用第三方或内置的模块是一种拿来主义,可以极大的提升开发效率。

    ②自定义模块即将我们自己程序中需要用到的公共的功能写入一个Python文件,然后程序的各部分组件可以通过导入的方式来引用/重用自定义模块中的功能。

    如何使用模块(how):

    导入的方式有两种:

    #方式一
    import 模块名
    #方式二
    from 模块名 import 具体功能

    方式一:import 模块名

    首次import m1导入模块都发生三件事:
    1、先创建一个模块的名称空间
    2、执行m1.py,将执行过程中产生的名称都放入模块的名称空间中
    3、在当前执行文件中拿到一个名字m1,该名字是指向模块的名称空间的

    使用方法:指名道姓地访问m1名称空间中的名字func,优点是不会与当前名称空间中的名字冲突,缺点是每次访问都需要加上前缀m1.func

    方式二:from 模块名import 具体功能

    首次from m1 import func导入模块都发生三件事:
    1、先创建一个模块的名称空间
    2、执行m1.py,将执行过程中产生的名称都放入模块的名称空间中
    3、在当前执行文件中直接拿到一个功能名func,该名字是直接指向模块名称空间中的某一个功能的

    使用方法:直接使用功能即可,优点是无需加任何前缀,缺点是容易与当前名称空间中的名字冲突
    def func():
    pass
    func()

    Tips:

    在被当做执行文件执行时,__name__='__main__'

    在被当做模块导入时,__name__='aaa'

    模块的搜索路径:

    一个py文件就是一个模块,在导入时,必须从某一个文件夹下找到该py文件。

    模块的搜索路径值得是在导入模块时需要检索的文件夹们。

    导入模块的查找顺序是:

    ①先从内存中已经导入的模块查找

    ②内置的模块

    ③环境变量中sys.path中找

    强调:sys.path的第一个值是当前执行文件所在的文件夹

    三、包

    什么是包(what):

    包是模块的一种形式,包的本质就是一个含有__init__.py的文件夹。

    为什么要有包(why):

    方便管理模块调用。

    如何使用包(how):

    导入包就是在导入包下的__init__.py

    import ...

    from ... import ...

    【注意点】

    1、包内所有的文件都是被导入使用的,而不是被直接运行的
    2、包内部模块之间的导入可以使用绝对导入(以包的根目录为基准)与相对导入(以当前被导入的模块所在的目录为基准)
    推荐使用相对导入
    3、当文件是执行文件时,无法在该文件内用相对导入的语法
    只有在文件时被当作模块导入时,该文件内才能使用相对导入的语法

    4、凡是在导入时带点的,点的左边都必须是一个包
    import aaa.bbb.m3.f3 # 错误

    四、logging模块(日志模块)
    1.日志级别
    CRITICAL = 50 #FATAL = CRITICAL
    ERROR = 40
    WARNING = 30 #WARN = WARNING
    INFO = 20
    DEBUG = 10
    NOTSET = 0 #不设置
    2.默认级别为warning,默认打印到终端
    import logging
    
    logging.debug('调试debug')
    logging.info('消息info')
    logging.warning('警告warn')
    logging.error('错误error')
    logging.critical('严重critical')
    
    '''
    WARNING:root:警告warn
    ERROR:root:错误error
    CRITICAL:root:严重critical
    '''

        3.logging模块的Formatter,Handler,Logger,Filter对象

    #logger:产生日志的对象
    
    #Filter:过滤日志的对象
    
    #Handler:接收日志然后控制打印到不同的地方,FileHandler用来打印到文件中,StreamHandler用来打印到终端
    
    #Formatter对象:可以定制不同的日志格式对象,然后绑定给不同的Handler对象使用,以此来控制不同的Handler的日志格式

        4.logging配置文件

    """
    logging配置
    """
    
    import os
    import logging.config
    
    # 定义三种日志输出格式 开始
    
    standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' 
                      '[%(levelname)s][%(message)s]' #其中name为getlogger指定的名字
    
    simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
    
    id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s'
    
    # 定义日志输出格式 结束
    
    logfile_dir = os.path.dirname(os.path.abspath(__file__))  # log文件的目录
    
    logfile_name = 'all2.log'  # log文件名
    
    # 如果不存在定义的日志目录就创建一个
    if not os.path.isdir(logfile_dir):
        os.mkdir(logfile_dir)
    
    # log文件的全路径
    logfile_path = os.path.join(logfile_dir, logfile_name)
    
    # log配置字典
    LOGGING_DIC = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'standard': {
                'format': standard_format
            },
            'simple': {
                'format': simple_format
            },
        },
        'filters': {},
        'handlers': {
            #打印到终端的日志
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',  # 打印到屏幕
                'formatter': 'simple'
            },
            #打印到文件的日志,收集info及以上的日志
            'default': {
                'level': 'DEBUG',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件
                'formatter': 'standard',
                'filename': logfile_path,  # 日志文件
                'maxBytes': 1024*1024*5,  # 日志大小 5M
                'backupCount': 5,
                'encoding': 'utf-8',  # 日志文件的编码,再也不用担心中文log乱码了
            },
        },
        'loggers': {
            #logging.getLogger(__name__)拿到的logger配置
            '': {
                'handlers': ['default', 'console'],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
                'level': 'DEBUG',
                'propagate': True,  # 向上(更高level的logger)传递
            },
        },
    }
    
    
    def load_my_logging_cfg():
        logging.config.dictConfig(LOGGING_DIC)  # 导入上面定义的logging配置
        logger = logging.getLogger(__name__)  # 生成一个log实例
        logger.info('It works!')  # 记录该文件的运行状态
    
    if __name__ == '__main__':
        load_my_logging_cfg()
    
    logging配置文件
    logging配置

    五、常用模块之——re模块

    什么是正则表达式(what):

    正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法。或者说:正则就是用来描述一类事物的规则。

    为什么要用正则表达式(why):

    用来描述一类事物的规则。

    ①常用匹配模式(元字符)

    # =================================匹配模式=================================
    #一对一的匹配
    # 'hello'.replace(old,new)
    # 'hello'.find('pattern')
    
    #正则匹配
    import re
    #w与W
    print(re.findall('w','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3']
    print(re.findall('W','hello egon 123')) #[' ', ' ']
    
    #s与S
    print(re.findall('s','hello  egon  123')) #[' ', ' ', ' ', ' ']
    print(re.findall('S','hello  egon  123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3']
    
    #
     	都是空,都可以被s匹配
    print(re.findall('s','hello 
     egon 	 123')) #[' ', '
    ', ' ', ' ', '	', ' ']
    
    #
    print(re.findall(r'
    ','hello egon 
    123')) #['
    ']
    print(re.findall(r'	','hello egon	123')) #['
    ']
    
    #d与D
    print(re.findall('d','hello egon 123')) #['1', '2', '3']
    print(re.findall('D','hello egon 123')) #['h', 'e', 'l', 'l', 'o', ' ', 'e', 'g', 'o', 'n', ' ']
    
    #A与
    print(re.findall('Ahe','hello egon 123')) #['he'],A==>^
    print(re.findall('123','hello egon 123')) #['he'],==>$
    
    #^与$
    print(re.findall('^h','hello egon 123')) #['h']
    print(re.findall('3$','hello egon 123')) #['3']
    
    # 重复匹配:| . | * | ? | .* | .*? | + | {n,m} |
    #.
    print(re.findall('a.b','a1b')) #['a1b']
    print(re.findall('a.b','a1b a*b a b aaab')) #['a1b', 'a*b', 'a b', 'aab']
    print(re.findall('a.b','a
    b')) #[]
    print(re.findall('a.b','a
    b',re.S)) #['a
    b']
    print(re.findall('a.b','a
    b',re.DOTALL)) #['a
    b']同上一条意思一样
    
    #*
    print(re.findall('ab*','bbbbbbb')) #[]
    print(re.findall('ab*','a')) #['a']
    print(re.findall('ab*','abbbb')) #['abbbb']
    
    #?
    print(re.findall('ab?','a')) #['a']
    print(re.findall('ab?','abbb')) #['ab']
    #匹配所有包含小数在内的数字
    print(re.findall('d+.?d*',"asdfasdf123as1.13dfa12adsf1asdf3")) #['123', '1.13', '12', '1', '3']
    
    #.*默认为贪婪匹配
    print(re.findall('a.*b','a1b22222222b')) #['a1b22222222b']
    
    #.*?为非贪婪匹配:推荐使用
    print(re.findall('a.*?b','a1b22222222b')) #['a1b']
    
    #+
    print(re.findall('ab+','a')) #[]
    print(re.findall('ab+','abbb')) #['abbb']
    
    #{n,m}
    print(re.findall('ab{2}','abbb')) #['abb']
    print(re.findall('ab{2,4}','abbb')) #['abb']
    print(re.findall('ab{1,}','abbb')) #'ab{1,}' ===> 'ab+'
    print(re.findall('ab{0,}','abbb')) #'ab{0,}' ===> 'ab*'
    
    #[]
    print(re.findall('a[1*-]b','a1b a*b a-b')) #[]内的都为普通字符了,且如果-没有被转意的话,应该放到[]的开头或结尾
    print(re.findall('a[^1*-]b','a1b a*b a-b a=b')) #[]内的^代表的意思是取反,所以结果为['a=b']
    print(re.findall('a[0-9]b','a1b a*b a-b a=b')) #[]内的^代表的意思是取反,所以结果为['a=b']
    print(re.findall('a[a-z]b','a1b a*b a-b a=b aeb')) #[]内的^代表的意思是取反,所以结果为['a=b']
    print(re.findall('a[a-zA-Z]b','a1b a*b a-b a=b aeb aEb')) #[]内的^代表的意思是取反,所以结果为['a=b']
    
    ## print(re.findall('a\c','ac')) #对于正则来说a\c确实可以匹配到ac,但是在python解释器读取a\c时,会发生转义,然后交给re去执行,所以抛出异常
    print(re.findall(r'a\c','ac')) #r代表告诉解释器使用rawstring,即原生字符串,把我们正则内的所有符号都当普通字符处理,不要转义
    print(re.findall('a\\c','ac')) #同上面的意思一样,和上面的结果一样都是['a\c']
    
    #():分组
    print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab']
    print(re.findall('(ab)+123','ababab123')) #['ab'],匹配到末尾的ab123中的ab
    print(re.findall('(?:ab)+123','ababab123')) #findall的结果不是匹配的全部内容,而是组内的内容,?:可以让结果为匹配的全部内容
    print(re.findall('href="(.*?)"','<a href="http://www.baidu.com">点击</a>'))#['http://www.baidu.com']
    print(re.findall('href="(?:.*?)"','<a href="http://www.baidu.com">点击</a>'))#['href="http://www.baidu.com"']
    
    #|
    print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company'))
    常用元字符应用

    ②search、match与split

    # ===========================re模块提供的方法介绍===========================
    import re
    #1
    print(re.findall('e','alex make love') )   #['e', 'e', 'e'],返回所有满足匹配条件的结果,放在列表里
    #2
    print(re.search('e','alex make love').group()) #e,只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。
    
    #3
    print(re.match('e','alex make love'))    #None,同search,不过在字符串开始处进行匹配,完全可以用search+^代替match
    
    #4
    print(re.split('[ab]','abcd'))     #['', '', 'cd'],先按'a'分割得到''和'bcd',再对''和'bcd'分别按'b'分割
    
    #5
    print('===>',re.sub('a','A','alex make love')) #===> Alex mAke love,不指定n,默认替换所有
    print('===>',re.sub('a','A','alex make love',1)) #===> Alex make love
    print('===>',re.sub('a','A','alex make love',2)) #===> Alex mAke love
    print('===>',re.sub('^(w+)(.*?s)(w+)(.*?s)(w+)(.*?)$',r'52341','alex make love')) #===> love make alex
    
    print('===>',re.subn('a','A','alex make love')) #===> ('Alex mAke love', 2),结果带有总共替换的个数
    
    
    #6
    obj=re.compile('d{2}')
    
    print(obj.search('abc123eeee').group()) #12
    print(obj.findall('abc123eeee')) #['12'],重用了obj
    search、match与split

    六、常用模块之——json模块与pickle模块

    什么是序列化与反序列化(what):

    序列化就是将内存中的数据结构转换成一种中间格式存储到硬盘或者基于网络传输。

    为什么要有序列化(why):

    ①可以保存程序运行状态

    ②可以数据跨平台交互

    如何使用序列化(how):

    import json

    优点:跨平台性强

    缺点:只能支持/对应python部分数据类型

    import pickle

    优点:可以支持/对应所有python的数据类型

    缺点:只能被python识别

    JSON表示的对象就是标准的JavaScript语言的对象,JSON和Python内置的数据类型对应如下:

    【json】

    工作原理:

    import json
     
    dic={'name':'alvin','age':23,'sex':'male'}
    print(type(dic))#<class 'dict'>
     
    j=json.dumps(dic)
    print(type(j))#<class 'str'>
     
     
    f=open('序列化对象','w')
    f.write(j)  #-------------------等价于json.dump(dic,f)
    f.close()
    #-----------------------------反序列化<br>
    import json
    f=open('序列化对象')
    data=json.loads(f.read())#  等价于data=json.load(f)
    import json
    #dct="{'1':111}"#json 不认单引号
    #dct=str({"1":111})#报错,因为生成的数据还是单引号:{'one': 1}
    
    dct='{"1":"111"}'
    print(json.loads(dct))
    
    #conclusion:
    #        无论数据是怎样创建的,只要满足json格式,就可以json.loads出来,不一定非要dumps的数据才能loads
    
     注意点
    注意点

    【pickle】

    工作原理:

    import pickle
     
    dic={'name':'alvin','age':23,'sex':'male'}
     
    print(type(dic))#<class 'dict'>
     
    j=pickle.dumps(dic)
    print(type(j))#<class 'bytes'>
     
     
    f=open('序列化对象_pickle','wb')#注意是w是写入str,wb是写入bytes,j是'bytes'
    f.write(j)  #-------------------等价于pickle.dump(dic,f)
     
    f.close()
    #-------------------------反序列化
    import pickle
    f=open('序列化对象_pickle','rb')
     
    data=pickle.loads(f.read())#  等价于data=pickle.load(f)
     
     
    print(data['age'])

    七、常用模块之——hashlib模块

    什么是hash(what):

    hash是一种算法,该算法接收传入的内容,经过运算得到一串hash值。

    为什么要用hash算法(why):

    hash值有三大特性:

    ①只要传入的内容一样,得到的hash值必然一样。

    ②只要hash算法固定,无论传入的内容有多大,得到的hash值长度是固定的。

    ③不可以用hash值逆推出原来的内容。

    基于①和②在下载文件时可以做文件一致性检测。

    基于①和③可以对密码进行加密。

    如何用hashlib模块(how):

    import hashlib
    
    #1、造出hash工厂
    m=hashlib.md5()
    
    #2、运送原材料
    m.update('你好啊美丽的'.encode('utf-8'))
    m.update('张铭言'.encode('utf-8'))
    
    #3、产出hash值
    print(m.hexdigest()) #66bcb9758826f562ae8cb70d277a4be9
    
    
    #1、造出hash工厂
    m=hashlib.md5(''.encode('utf-8'))
    
    #2、运送原材料
    m.update('好啊美丽的张铭言'.encode('utf-8'))
    
    #3、产出hash值
    print(m.hexdigest()) #66bcb9758826f562ae8cb70d277a4be9
    
    
    
    应用一:文件一致性校验
    #1、造出hash工厂
    m=hashlib.sha512(''.encode('utf-8'))
    
    #2、运送原材料
    m.update('好啊美sadfsadf丽asdfsafdasdasdfsafsdafasdfasdfsadfsadfsadfsadfasdff的张铭言'.encode('utf-8'))
    
    
    #3、产出hash值
    print(m.hexdigest()) #2ff39b418bfc084c8f9a237d11b9da6d5c6c0fb6bebcde2ba43a433dc823966c
    
    
    #1、造出hash工厂
    m=hashlib.md5()
    
    #2、运送原材料
    with open(r'E:1.mp4','rb') as f:
        for line in f:
            m.update(line)
    #3、产出hash值
    print(m.hexdigest()) #1273d366d2fe2dc57645cc1031414a05
    #                     1273d366d2fe2dc57645cc1031414a05
    
    应用一:对明文密码进行加密
    password=input('>>>: ')
    
    m=hashlib.md5()
    m.update('天王盖地虎'.encode('utf-8'))
    m.update(password.encode('utf-8'))
    print(m.hexdigest()) #95bd6eafefdf51d8b153785f3fb6263d
    
    import hmac
    
    m=hmac.new('小鸡炖蘑菇'.encode('utf-8'))
    m.update('hello'.encode('utf-8'))
    print(m.hexdigest())

    八、常用模块之——time模块与datatime模块

    在Python中,通常有这几种方式来表示时间:

    • 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
    • 格式化的时间字符串(Format String)
    • 结构化的时间(struct_time):struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时
    import time
    #--------------------------我们先以当前时间为准,让大家快速认识三种形式的时间
    print(time.time()) # 时间戳:1487130156.419527
    print(time.strftime("%Y-%m-%d %X")) #格式化的时间字符串:'2017-02-15 11:40:53'
    
    print(time.localtime()) #本地时区的struct_time
    print(time.gmtime())    #UTC时区的struct_time

    其中计算机认识的时间只能是'时间戳'格式,而程序员可处理的或者说人类能看懂的时间有: '格式化的时间字符串','结构化的时间' ,于是有了下图的转换关系。

    #--------------------------按图1转换时间
    # localtime([secs])
    # 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
    time.localtime()
    time.localtime(1473525444.037215)
    
    # gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
    
    # mktime(t) : 将一个struct_time转化为时间戳。
    print(time.mktime(time.localtime()))#1473525749.0
    
    
    # strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和
    # time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个
    # 元素越界,ValueError的错误将会被抛出。
    print(time.strftime("%Y-%m-%d %X", time.localtime()))#2016-09-11 00:49:56
    
    # time.strptime(string[, format])
    # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
    print(time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X'))
    #time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,
    #  tm_wday=3, tm_yday=125, tm_isdst=-1)
    #在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。

    #--------------------------按图2转换时间
    # asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。
    # 如果没有参数,将会将time.localtime()作为参数传入。
    print(time.asctime())#Sun Sep 11 00:43:43 2016
    
    # ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为
    # None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
    print(time.ctime())  # Sun Sep 11 00:46:38 2016
    print(time.ctime(time.time()))  # Sun Sep 11 00:46:38 2016

    【datetime模块】

    #时间加减
    import datetime
    
    # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
    #print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
    # print(datetime.datetime.now() )
    # print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
    # print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
    # print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
    # print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
    
    
    #
    # c_time  = datetime.datetime.now()
    # print(c_time.replace(minute=3,hour=2)) #时间替换

    九、常用模块之——os模块与sys模块

    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    输出用于分割文件路径的字符串 win下为;,Linux下为:
    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所指向的文件或者目录的最后修改时间
    os.path.getsize(path) 返回path的大小
    常用操作

    【路径处理】

    os路径处理
    #方式一:
    import os
    #具体应用
    import os,sys
    possible_topdir = os.path.normpath(os.path.join(
        os.path.abspath(__file__),
        os.pardir, #上一级
        os.pardir,
        os.pardir
    ))
    sys.path.insert(0,possible_topdir)
    
    
    #方式二:
    os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

    十、常用模块之——random模块与suprocess模块

    【random】

    常用操作

    【生成随机验证码】

    import random
    
    def make_random_code(n):
        res = ''
        for i in range(n):
            alpha = chr(random.randint(65,90))
            digital = str(random.randint(0,9))
            res += random.choice([alpha,digital])
        return res
    print(make_random_code(6))

    【suprocess】

    import  subprocess
    
    '''
    sh-3.2# ls /Users/egon/Desktop |grep txt$
    mysql.txt
    tt.txt
    事物.txt
    '''
    
    res1=subprocess.Popen('ls /Users/jieli/Desktop',shell=True,stdout=subprocess.PIPE)
    res=subprocess.Popen('grep txt$',shell=True,stdin=res1.stdout,
                     stdout=subprocess.PIPE)
    
    print(res.stdout.read().decode('utf-8'))
    
    
    #等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep
    res1=subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',shell=True,stdout=subprocess.PIPE)
    print(res1.stdout.read().decode('utf-8'))
    
    
    #windows下:
    # dir | findstr 'test*'
    # dir | findstr 'txt$'
    import subprocess
    res1=subprocess.Popen(r'dir C:UsersAdministratorPycharmProjects	est函数备课',shell=True,stdout=subprocess.PIPE)
    res=subprocess.Popen('findstr test*',shell=True,stdin=res1.stdout,
                     stdout=subprocess.PIPE)
    
    print(res.stdout.read().decode('gbk')) #subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码
    常用操作

    【打印进度条】

    import time
    
    def make_process(percent,width=50):
        if percent>1:percent=1
        show_str = ('[%%-%ds]' %width) %(int(percent*width)*'#')
        print('
    %s %s%%' %(show_str,int(percent*100)),end='')
    
    
    total = 102400
    down = 0
    while down<total:
        time.sleep(0.2)
        down+=1024
        percent = down/total
        make_process(percent)

    十一、其他模块——shutil模块、shelve模块、xml模块、configparser模块

    【shutil模块】

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

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

    1 import shutil
    2  
    3 shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))

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

    1 shutil.copyfile('f1.log', 'f2.log') #目标文件无需存在

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

    1 shutil.copymode('f1.log', 'f2.log') #目标文件必须存在

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

    1 shutil.copystat('f1.log', 'f2.log') #目标文件必须存在

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

    1 import shutil
    2  
    3 shutil.copy('f1.log', 'f2.log')

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

    1 import shutil
    2  
    3 shutil.copy2('f1.log', 'f2.log')

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

    1 import shutil
    2  
    3 shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) #目标目录不能存在,注意对folder2目录父级目录要有可写权限,ignore的意思是排除 
     拷贝软连接

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

    1 import shutil
    2  
    3 shutil.rmtree('folder1')

    shutil.move(src, dst)
    递归的去移动文件,它类似mv命令,其实就是重命名。

    1 import shutil
    2  
    3 shutil.move('folder1', 'folder3')

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

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

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

    • base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
      如 data_bak                       =>保存至当前路径
      如:/tmp/data_bak =>保存至/tmp/
    • format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
    • root_dir: 要压缩的文件夹路径(默认当前目录)
    • owner: 用户,默认当前用户
    • group: 组,默认当前组
    • logger: 用于记录日志,通常是logging.Logger对象
    复制代码
    1 #将 /data 下的文件打包放置当前程序目录
    2 import shutil
    3 ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data')
    4   
    5   
    6 #将 /data下的文件打包放置 /tmp/目录
    7 import shutil
    8 ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data') 
    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(path='.')
    z.close()
    
    zipfile压缩解压缩
    zip压缩解压缩
     【shelve模块】

     shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型。

    import shelve
    
    f=shelve.open(r'sheve.txt')
    # f['stu1_info']={'name':'egon','age':18,'hobby':['piao','smoking','drinking']}
    # f['stu2_info']={'name':'gangdan','age':53}
    # f['school_info']={'website':'http://www.pypy.org','city':'beijing'}
    
    print(f['stu1_info']['hobby'])
    f.close()

        【xml模块】

    xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的接口还主要是xml。

    <?xml version="1.0"?>
    <data>
        <country name="Liechtenstein">
            <rank updated="yes">2</rank>
            <year>2008</year>
            <gdppc>141100</gdppc>
            <neighbor name="Austria" direction="E"/>
            <neighbor name="Switzerland" direction="W"/>
        </country>
        <country name="Singapore">
            <rank updated="yes">5</rank>
            <year>2011</year>
            <gdppc>59900</gdppc>
            <neighbor name="Malaysia" direction="N"/>
        </country>
        <country name="Panama">
            <rank updated="yes">69</rank>
            <year>2011</year>
            <gdppc>13600</gdppc>
            <neighbor name="Costa Rica" direction="W"/>
            <neighbor name="Colombia" direction="E"/>
        </country>
    </data>
    xml文件
    <?xml version="1.0"?>
    <data>
        <country name="Liechtenstein">
            <rank updated="yes">2</rank>
            <year>2008</year>
            <gdppc>141100</gdppc>
            <neighbor name="Austria" direction="E"/>
            <neighbor name="Switzerland" direction="W"/>
        </country>
        <country name="Singapore">
            <rank updated="yes">5</rank>
            <year>2011</year>
            <gdppc>59900</gdppc>
            <neighbor name="Malaysia" direction="N"/>
        </country>
        <country name="Panama">
            <rank updated="yes">69</rank>
            <year>2011</year>
            <gdppc>13600</gdppc>
            <neighbor name="Costa Rica" direction="W"/>
            <neighbor name="Colombia" direction="E"/>
        </country>
    </data>
    常用操作1
    #在country内添加(append)节点year2
    import xml.etree.ElementTree as ET
    tree = ET.parse("a.xml")
    root=tree.getroot()
    for country in root.findall('country'):
        for year in country.findall('year'):
            if int(year.text) > 2000:
                year2=ET.Element('year2')
                year2.text='新年'
                year2.attrib={'update':'yes'}
                country.append(year2) #往country节点下添加子节点
    
    tree.write('a.xml.swap')
    常用操作2
    
    

        【configparser模块】

    # 注释1
    ; 注释2
    
    [section1]
    k1 = v1
    k2:v2
    user=egon
    age=18
    is_admin=true
    salary=31
    
    [section2]
    k1 = v1
    配置config.ini
    import configparser
    
    config=configparser.ConfigParser()
    config.read('a.cfg')
    
    #查看所有的标题
    res=config.sections() #['section1', 'section2']
    print(res)
    
    #查看标题section1下所有key=value的key
    options=config.options('section1')
    print(options) #['k1', 'k2', 'user', 'age', 'is_admin', 'salary']
    
    #查看标题section1下所有key=value的(key,value)格式
    item_list=config.items('section1')
    print(item_list) #[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]
    
    #查看标题section1下user的值=>字符串格式
    val=config.get('section1','user')
    print(val) #egon
    
    #查看标题section1下age的值=>整数格式
    val1=config.getint('section1','age')
    print(val1) #18
    
    #查看标题section1下is_admin的值=>布尔值格式
    val2=config.getboolean('section1','is_admin')
    print(val2) #True
    
    #查看标题section1下salary的值=>浮点型格式
    val3=config.getfloat('section1','salary')
    print(val3) #31.0
    读取config.ini
    import configparser
    
    config=configparser.ConfigParser()
    config.read('a.cfg',encoding='utf-8')
    
    
    #删除整个标题section2
    config.remove_section('section2')
    
    #删除标题section1下的某个k1和k2
    config.remove_option('section1','k1')
    config.remove_option('section1','k2')
    
    #判断是否存在某个标题
    print(config.has_section('section1'))
    
    #判断标题section1下是否有user
    print(config.has_option('section1',''))
    
    
    #添加一个标题
    config.add_section('egon')
    
    #在标题egon下添加name=egon,age=18的配置
    config.set('egon','name','egon')
    config.set('egon','age',18) #报错,必须是字符串
    
    
    #最后将修改的内容写入文件,完成最终的修改
    config.write(open('a.cfg','w'))
    改写config.ini
     
  • 相关阅读:
    获取网络时间,减轻自己服务器的请求压力
    mysql学习记录(windows)
    Docker 部署本地pip源
    npm : 无法加载文件 D:vueProject odejs ode_global pm.ps1
    微信小程序没有找到可构建的npm包
    vue 记录 mode:history 模式 踩过的坑
    Linux学习笔记
    kafka监控 Kafka-eagle-web
    vi 分屏 --(visual 可视模式)
    [安卓网络入门] 获取天气
  • 原文地址:https://www.cnblogs.com/neymargoal/p/9198631.html
Copyright © 2011-2022 走看看