20 内置模块
20.1 os、sys模块
import os
res = os.getenv('path') #读取环境变量
print(res)
os.path.split(r"D:\\zhangsan\\hello\\test.py") #把路径分为两部分,第二部分使目录路径,第二部分是文件名
os.path.dirname("xxx") #显示路径名
os.path.basename("xxx") #显示split分割的第二个元素
os.path.exists("xxx") #判断路径是否存在,存在返回True,不存在返回False
os.path.isfile("xxx") #判断一个文件是否存在
os.path.isdir("xxx") #判断文件夹是否存在
# sys模块,负责程序跟python解释器的交互
import sys
print(sys.getdefaultencoding()) #获取系统当前的编码
print(sys.path) #返回环境变量的路径,跟解释器有关的
print(sys.platform) #返回当前系统平台
print(sys.version) #查看目前python的版本
20.2 time模块
import time
t1 = time.time() #获取到时间戳,从1970年开始,到现在的
print(t1,type(t1)) # 时间戳,float类型
t2 = time.localtime()
print(time.localtime()) #转换为年月日小时、分钟、秒
print(t2[0])
print(t2.tm_year)
for i in t2:
print(i)
# time.asctime()
print(time.asctime()) #转换为更通俗的方式
# Sat Jun 19 21:27:53 2021
# time.strftime(格式化字符串,struct_time 对象)
# time.strptime(时间字符串,格式化字符串)
# 将对象转换为时间字符串
tf = time.strftime("%Y-%m-%d %H:%M:%s",time.localtime())
print(tf) # 2021-06-19
# 将时间字符串转换为struct_time对象
stime = '2019-12-23' #定义时间字符串
tp = time.strptime(stime, "%Y-%m-%d")
print(tp)
20.3 pyinstaller 模块
把python解释器和脚本打包成一个可执行文件
打包的执行文件,只能在和打包系统同样的环境下执行
安装命令 pip install 模块名
pip install pyinstaller
打包命令
pyinstaller -F xx.py
在dist文件夹里面有exe文件
-F 产生单个的可执行exe文件
20.4 hashlib模块
加密模块,hash加密
import hashlib
# 进行md5加密 第二个参数必须是二进制的形式
st = "我命油我不由天"
res = hashlib.new('md5', st.encode())
print(res)
# 显示哈希算法返回的结果
print(res.digest())
# 十六进制
print(res.hexdigest()) #显示为16进制
# hashlib.new 和 hashlib.md5 写法不同,功能一样
t = "你把握不住,孩子"
res2 = hashlib.md5(t.encode())
print(res2)
# 更新hash对象 在原来的对象后追加
res2.update('123'.encode()) #新加的内容,进行重新加密
print("更新后:",res2.hexdigest())