01os模块
#1.os模块 - 用来提供文件和文件夹相关操作的模块
import os
#1) os.getcwd() - 获取当前目录
print(os.getcwd()) #E:\千峰学习\01语言基础\Day16-常用的系统模块
open(os.getcwd()+r'\test.py')
#2.os.listdir(文件夹路径) - 获取指定文件夹下所有的内容和文件夹的名字(获取指定文件夹下所有的内容)
#练习:读取files文件夹中所有的文件内容
print(os.listdir(r'.\files'))
for file_name in os.listdir(r'.\files'):
result=open('.\\files\\'+ file_name).read()
print(result)
#练习.读取files中所有txt文件的内容
for file_name in os.listdir(r'.\files'):
if file_name[-4:]=='.txt':
result=open(r'\.files\'+file_name).read()
print(result)
#3.创建文件夹 - mkdir、makedirs
os.md
os.mkdir(r'.\files\a')
os.makedirs(r'.\files\aaa\bbb')
#4.os.path.basename(文件路径) - 返回文件名
result=os.path.basename(r'.files\file1.txt')
print(result)
#5.os.path.exists(文件\文件路径) - 判断指定文件或者文件夹是否存在
result=os.path.exists(r'.\files')
print(result)
result=os.path.exists(r'\files\file11.txt')
print(result)
#6.)os.path.splitext(文件路径) - 文件名和后缀分离
print(os.path.splitext(r'.\files\file1.txt'))
print(os.listdir('.\\files\\'))
02 数学模块和随机模块
#1.数学模块
import math #这里面所有的工具都是针对普通数字的
import cmath #这里面所有的工具都是针对复数的
#python中和数字相关的类型有4种:int、float、bool、complex
# ai+b - 复数,i叫虚数单位,i**2==-1
# 注意:python中的复数,j是虚数单位
x=2j+3
y=3j-4
print(x+y) #(-1+5j)
print(x*y) #(-18+1j)
#math 模块
#1)小数转整数
#math.ceil(数字) -向大取整
#math.floor(数字) -向小取整
#round(数字) -四舍五入
print(math.ceil(1.9)) #2
print(math.ceil(1.1)) #2
print(math.floor(1.9)) #1
print(math.floor(1.1)) #1
print(round(1.9)) #2
print(round(1.1)) #1
#2)求绝对值
# abs(数字)
# math.fabs(数字)
print(abs(-30)) #30
print(math.fabs(-30)) #30.0
print(abs(-30.56)) #30.56
print(math.fabs(-30.56)) #30.56
#2.随机模块
import random
#1.产生随机整数:random.randint(a,b) - 产生a到b的随机整数[a,b]
print(random.randint(1,2))
#2.产生随机小数:random.random() - 产生0~1的随机小数
print(random.random())
#产生0~100的随机小数
print(random.random()*100)
#产生50~100的随机小数
print(random.random()*100+50)
#产生M~N的随机小数:random.random()*(N-M)+M
#3.在指定的等差数列中随机获得一个数:random.randrange(起点,终点,步长)-[起点,终点)
#产生0~100的随机偶数
print(random.randrange(0,101,2))
#4)洗牌(随机打乱列表中元素中的顺序)
nums=[10,20,30,40,50,60]
random.shuffle(nums)
print(nums)
#5)抽牌
#random.choice(序列) - 随机获取一个元素
#random.choices(序列,k=数量) - 随机获取多个元素(有放回)
#random.sample(序列,k=数量) - 随机获取多个元素(无放回)
nums=[10,20,30,40,50,60]
result=random.choices(nums,cum_weights=[1,2,1,5,6,888],k=4)
print(result)
result=random.sample(nums,k=4)
print(result)
03哈希摘要
1.哈希加密(摘要)
1)密文不可逆
2)相同的数据通过相同的算法得到的密文相同,不同的数据通过相同的算法得到的密文不相同
3)不同长度的数据通过相同的算法得到的密文长度相同
2.Python生成哈希摘要的方法
import hashlib
#1)根据算法创建hash对象
#常用hash算法:mad5和shaxxx
hash=hashlib.md5()
#2)添加生成摘要数据
#hash对象.update(二进制数据)
'''
Python中字符串和二进制的相互转换: 二进制 - bytes
a.字符串转二进制
方法一:bytes(字符串,'utf-8')
方法二:字符串.encode()
b.二进制转字符串
方法一:str(二进制,'utf-8')
方法二:二进制.decode()
'''
#生成文本数据的摘要
# hash.update(bytes('123456','utf-8'))
hash.update('123456'.encode())
#3.获取摘要值(获取密文)
result=hash.hexdigest()
print(result)
04time模块
#补充:时间戳
'''
时间戳指的是某一个时间到1970年1月1日0时0秒(格林威治时间)的时间差,单位是秒
'''
#1.time.time() - 获取当前时间(返回的是当前时间的时间戳)
t1=time.time()
print(t1)
#2.time.localtime()
#time.localtime() - 获取当前时间(返回的时间的是本地时间对应的结构体时间)
#time.localtime(时间戳) - 将时间戳转换成结构体时间
t2=time.localtime()
print(t2)
#通过结构体时间单独获取指定信息
#tm_wday - 星期值,从0~6表示周一~周天
print(t2.tm_year,t2.tm_mon)
#3.time.sleep(时间) - 让程序暂停\等待指定时间(单位:秒)
time.sleep(2)
print('=================')
#结构体时间和字符串时间的相互转换
#4.time.strftime(时间格式,结构体时间) - 将结构体时间转换成指定格式的字符串时间
result=time.strftime('%p %I:%M',t2)
print(result)
#5.time.strptime(时间字符串,时间格式) - 将字符串时间转换成结构体时间
#'2017-3-24' 是周几?
result=time.strptime('2017-3-24','%Y-%m-%d')
print(result)
#6.time.mktime(结构体时间) - 将结构体时间转化为时间戳
time.mktime()
05datetime
from datetime import datetime,date,time,timedelta
#1.创建datetime类型的数据
#1)获取当前时间
t1=datetime.now()
print(t1)
t2=datetime.today()
print(t2)
#2)直接提供时间信息
#datetime(年,月,日,时=0,分=0,秒=0)
t3=datetime(2018,10,1)
print(t3)
#3)通过字符串时间创建时间对象
t4=datetime.strptime('2019-3-22 23:30:24','%Y-%m-%d %H:%M:%S')
print(t4)
#2.获取时间对象具体的时间信息
print(t4.year)
print(t4.month)
...
#3.时间的加减操作
#时间对象 -\+ timedelta(days=?,hours=?,minutes=?,seconds=?,weeks=?)