zoukankan      html  css  js  c++  java
  • day17 内置模块

     
    一 os模块:系统相关的模块
        1 和文件夹相关
     
    os.makedirs('dirname1/dirname2')    可生成多层递归目录  ***
    os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 ***
    os.mkdir('dirname')    生成单级目录;相当于shell中mkdir dirname ***
    os.rmdir('dirname')    删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname ***
    os.listdir('dirname')    列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 **
     
        2 和文件相关
     
    os.remove()  删除一个文件  ***
    os.rename("oldname","newname")  重命名文件/目录  ***
    os.stat('path/filename')  获取文件/目录信息 **
     
        3 和操作系统差异相关
     
    os.sep    输出操作系统特定的路径分隔符,win下为"\",Linux下为"/" *
    os.linesep    输出当前平台使用的行终止符,win下为" ",Linux下为" " *
    os.pathsep    输出用于分割文件路径的字符串 win下为;,Linux下为: *
    os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix' *
     
    4 路径相关
     
    os.path.abspath(path) 返回path规范化的绝对路径 ***// print(os.path.abspath("H:\python-project\venv\Scripts")) 返回值为H:python-projectvenvScripts
    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的大小 ***
     
    二 sys模块
     
    sys.argv 命令行参数List,第一个元素是程序本身路径
    sys.exit(n) 退出程序,正常退出时exit(0),错误退出sys.exit(1)
    sys.version 获取Python解释程序的版本信息
    sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值 ***
    sys.platform 返回操作系统平台名称
     
    三 hashlib模块
        1 用作加密
    eg1:
            import hashlib
            md5=hashlib.md5()    //获取一个md5对象
            md5.update('123456'.encode('utf-8'))
            print(md5.hexdigest())    //e10adc3949ba59abbe56e057f20f883e 对同一个密码加密的结果是唯一的。
     
    eg2: 加salt
            import hashlib
            ret=hashlib.md5('要加的盐'.encode('utf-8'))
            ret.update('abcde'.encode('utf-8'))
            print(ret.hexdigest())    //c30fcda82236a500fa87a84599ce9151
     
        2 用作文件一致性校验
        def file_check(file_path):
            with open(file_path,encode='utf-8',mode='rb') as f1:
                sha256=hashlib.sha256()
                while 1:
                    content=f1.read(1024)
                    if content:
                        sha256.update(content)
                    else:
                        return sha256.hexdigest()
     
    四 collections模块 提供了几个额外的数据类型Counter、deque、defaultdict、namedtuple和OrderedDict
     
    1.namedtuple: 生成可以使用名字来访问元素内容的tuple
        eg:
            from collections import nametuple
            Point=nametuple('Point',['x','y'])
            p=Point(1,2)
            print(p.x,p.y)    //1,2
            p=Point(2,1)
            print(p.x,p.y)    //2,1
     
    2.deque: 双端队列,可以快速的从另外一侧追加和推出对象
        eg:
            from collections import deque
            q=deque(['a'],['b'],['c'])
            q.append('x')
            q.appendleft('y')    //从头插入
            q.popleft()    //从头删除
            
     
    3.Counter: 计数器,主要用来计数。是一个无序的容器类型,以字典键值对形式存储,可以输出
        eg:
             from collections import Counter
             count=Counter('123213454632345')
             print(count)
    4.OrderedDict: 有序字典    3.6之后版本dict也是有序的了
    5.defaultdict: 带有默认值的字典
        eg:
            from collections import defaultdict
            dd=defaultdict(lambda: 'N/A')
            dd['key']='ss'
            print(dd['key'])    //ss
            print(dd['key2'])    //N/A
     
    五 random模块
        random.random()    //获取[0.0,1.0)之间的任意小数
        random.randint(a,b)    //获取[a,b)范围内的一个整数
        random.uniform(a,b)    //获取[a,b)范围内的任意小数
        random.shuffle(x)    //把参数指定的数据中的元素顺序打乱。参数必须是可变的数据类型
        random.sample(x,k)    //从x中随机抽取k个元素,组成一个列表返回
            eg:
                import random
            l1=[1,3,4,5,6,7,8]
            print(random.sample(l1,4))    //返回[7,8,5,4]
     
    六 time模块
        time.time() //获取时间戳
        time.gmtime() //获取格式化的时间对象,有9个字段 <class 'time.struct_time'>
        time.localtime() //获取格式化时间对象,有9个字段。和上面的函数都可以接受参数,就是time函数获取到的秒,可以根据秒参数得出对应的时间
        time.mktime() //将时间对象转换成时间戳 就是time函数获取到的时间戳
        time.strftime(format[,t])    //把时间对象格式转化成字符串
        time.strptime(str,format)    //把时间字符串转化成时间对象
        time.sleep(x)    //休眠x秒
    eg1:时间对象转换成字符串
    a=time.localtime()
    timestr=time.strftime("时间:%Y %m %d %H %M %S")
    print(timestr)    //时间:2020 06 12 23 12 01
     
    eg2:字符串转化成时间对象
    timeobj=time.strptime('2020 06 12','%Y %m %d')
    print(timeobj)
     
  • 相关阅读:
    MySQL limit 分页查询优化(百万级优化)
    HAVING 搜索条件在进行分组操作之后应用
    Mysql 多表连接查询 inner join 和 outer join 的使用
    php7 configure: error: Cannot find OpenSSL's <evp.h> 问题解决
    解决Cannot find config.m4 Make sure that you run '/home/php/bin/phpize' in the top level source directory of the module
    Ubuntu下彻底卸载默认安装的mysql,自己手动下载安装MYSQL
    mysql 运算操作符
    mysql 函数表
    【mysql】连接和断开服务器
    mysql 5.7 增删改查及别名的用法
  • 原文地址:https://www.cnblogs.com/spacetime-party/p/13081913.html
Copyright © 2011-2022 走看看