Python——常见模块

模块(module): 在python中,xxx.py就是一个模块

常见的系统内置模块的使用
    |-- math
    |-- random
    |-- uuid

    |-- os

    |-- sys
    |-- time
    |-- datetime
    |-- calendar
    |-- hashlib
    |-- hmac
    |-- base64
 

如何导入模块
 
import xxxx    import random
            import uuid, sys
            import os.path as 
from 包路径 import 模块名称
from 包路径.模块名称 import 方法、类、变量
from functools import partial

1、math:

该模块主要用于数学和相关计算的一个模块

进行数学计算
|-- math.ceil(num)        # 向上取整
|-- math.floor(num)        # 向下取整
|-- math.abfs(num)        # 求绝对值
|-- math.modf(num)        # 以元组的形式返回整数和小数部分
|-- math.pi                # 圆周率
|-- math.pow(num, num2)    # 幂次方 num**num2
|-- math.sqrt(num)        # 开平方根
属性:
    pi            # 圆周率
    e            # 自然常数
方法:
    三角函数         sin cos ...
    对数             log log10 ...
    ceil()            # 向上取整
    floor()            # 向下取整
    fabs            # 绝对值
    sqrt            # 开平方
    pow                # 幂次方
 

2、os

os.path模块
    'abspath', 'altsep', 'basename', 'commonpath', 'commonprefix',
    'curdir', 'defpath', 'devnull', 'dirname', 'exists', 'expanduser', 
    'expandvars', 'extsep', 'genericpath', 'getatime', 'getctime', 'getmtime',
    'getsize', 'isabs', 'isdir', 'isfile', 'islink', 'ismount', 'join', 
    'lexists', 'normcase', 'normpath', 'os', 'pardir', 'pathsep', 'realpath',
    'relpath', 'samefile', 'sameopenfile', 'samestat', 'sep', 'split',
    'splitdrive', 'splitext', 'stat', 'supports_unicode_filenames', 'sys']

主要管理操作系统文件管理系统
|-- chdir(path)                # 修改当前工作空间路径的
|-- curdir                       # 当前工作空间的路径
|-- os.getcwd()                # 获取路径的绝对路径
|-- os.path.abspath(path)    # 获取路径的绝对路径
|-- os.cpu_count()            # 获取当前电脑的CPU架构
|-- os.device_encoding(fd)    # 获取设备的编码
|-- os.getpid()                # 获取进程编号
|-- os.getppid()            # 获取当前进程的父进程编号
|-- os.kill()                # 通过编号杀死进程
|-- os.listdir(path)        # 遍历path路径下的所有文件和文件夹,以列表形式返回名称
|-- os.mkdir()                # 创建一个单层文件夹(目录)
|-- os.makedirs()            # 创建多层目录
|-- os.name                    # 获取操作系统名称
|-- os.rename()                # 重命名
|-- os.remove()                # 移除文件
|-- os.removes()            # 移除多层目录
|-- os.rmdir()                # 删除目录
|-- os.sep                    # 当前系统对应的文件分隔符
|-- abspath            # 获取绝对路径 dirname + basename
|-- altsep            # 文件分隔符
 |-- basename            # 文件名称
 |-- dirname            # 所属目录
 |-- exists()
 |-- join()
 |-- split()    

3、os.path

|-- os.path.abspath(path)        # 获取path的绝对路径
|-- os.path.altsep                # 路径的分割符
|-- os.path.basename(path)        # 获取路径对应的文件名称
|-- os.path.dirname(path)        # 获取文件所在目录
|-- os.path.exists(path)        # 判断文件是否存在
|-- os.path.getatime(path)        # 获取文件最后的访问时间
|-- os.path.getctime(path)        # 获取文件的创建时间
|-- os.path.getmtime(path)        # 获取文件的修改时间 
|-- os.path.getsize(path)        # 获取文件大小,单位是B(字节)
|-- os.path.isdir(path)            # 判断path是不是目录
|-- os.path.isfile(path)        # 判断路径是不是一个文件
|-- os.path.isabs(path)            # 判断路径是不是一个绝对路径
|-- os.path.islink(path)        # 判断路径是不是一个链接(软连接、硬链接)
|-- os.path.ismount(path)        # 判断路径是不是一个挂载文件(U盘、光驱等)
|-- os.path.join(path1, path2)    # 完成路径的拼接,会自动根据系统生成分隔符
|-- os.path.split(path)            # 分隔文件路径
 

4、sys

    |-- argv            # 脚本传参
    |-- exit()            # 退出系统
    |-- getdefaultencoding()    # 获取系统的默认编码
    |-- getfilesystemencoding()    # 获取文件编码
    |-- setrecursionlimit(num)    # 设置递归的极限值
    |-- getrecursionlimit()        # 获取递归的极限值
    |-- getrefcount()        # 获取对象的引用计数

5、random

    random.random()            # 随机[0~1) 区间
    random.randint()        # [a, b] 区间的随机整数
    random.uniform            # [a, b] 区间的随机数
    random.choice(容器)        # 随机选择一个元素
    random.shuffle(容器)        # 洗牌,随机打散数据

6、time

 'sleep', 'strftime', 'strptime', 'struct_time', 'thread_time',
   'thread_time_ns', 'time', 'time_ns', 'timezone', 'tzname']


    |-- asctime()                # 返回一个日期字符串 
    |-- ctime()                # 返回一个日期字符串 
    |-- gmtime()                # 获取一个时间对象
    |-- localtime()                # 获取一个时间对象
    |-- time()                # 获取当前时间戳
    |-- sleep(sec)                # 休眠
    |-- strftime()                # 格式化时间
    |-- strptime()                # 将一个字符串格式时间,转换为日期对象

7、datetime

    |-- time([hour[,minutes][,seconds]])    # 构建一个时间对象
    |-- date(year,month,day)        # 构建一个日期对象
    |-- datetime子模块

    

8、datetime子模块

    import datetime
        datetime.datetime.xxxx()
    from datetime import datetime

    'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fold',
    'fromisocalendar', 'fromisoformat', 'fromordinal', 'fromtimestamp',
    'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 
    'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second',
    'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz', 
    'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 
    'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year']

|-- astimezone(时间对象)        # 返回当前时区对应的时间对象
|-- datetime.now()            # 返回当前时间对象
|-- ctime(时间对象)            # 一个符合欧美标准的时间字符串
|-- time(时间对象)            # 返回时间对象的时间部分
|-- date(时间对象)            # 返回时间对象的日期部分
|-- day                        #一周
|-- now                        #时间
|-- minute                    #分钟
|-- second                    #秒
|-- microsecond                # 微秒

9、calendar模块

日历模块

10、hashlib

加密学:
        如果通过秘钥是否同一个,可以将加密算法:
            |-- 对称加密
                DES

            |-- 非对称加密
                加密的秘钥和解密秘钥并不是使用同一个
                rsa
                生成一对秘钥,公钥和私钥
            
            |-- 哈希加密(hash)、散列加密
                不可逆加密、单向、结果是唯一的
                md5
                shal256
                shal384

11、hmac

    import hmac
    m = hmac.new("123456".encode("utf-8"), "liujianhong".encode("utf-8"), "MD5")
    m.hexdigest()

12、uuid:

具有唯一性

import uuid
s = uuid.uuid4()
s.hex
 

猜你喜欢

转载自blog.csdn.net/weixin_45802686/article/details/109151278