day14常用模块
一 os模块的作用
1 os模块的作用 import os
os模块 - 文件或者文件夹(目录)或者路径相关操作
例如:创建文件夹、创建文件、删除文件、判断文件是否存在、获取绝路径、获取一个文件夹中所有的文件等等
2 os模块常用方法
1)返回当前工作目录 print(os.getcwd())
2)获取指定文件夹中所有的内容的名字 os.listdir(文件夹路径)
3)创建文件夹
os.mkdir(文件夹路径) - 在指定路径下创建指定的文件夹(整个路径中只有最后的那个文件夹不存在才可以创建)
os.makedirs(文件夹路径) - 递归创建文件夹 (可以创建路径中所有不存在的文件夹)
4)返回绝对路径
绝对路径:文件或者文件夹在计算机中全路径(windows电脑绝对路径从盘开始写)
相对路径:用.表示当前目录(当前目录指的是当前代码文件所在的文件夹)
用…表示当前目录的上层目录
os.path.abspath(相对路径) - 返回相对路径对应的绝对路径
5)获取文件名
os.path.basename(文件路径) - 获取文件路径中的文件名
6)检测路径是否有效
os.path.exists()
7)判断是否是文件或者文件夹
os.path.isfile(路径) - 是否是文件
os.path.isdir(路径) - 是否是文件夹
8)把目录和文件名合成一个路径
os.path.join(文件夹路径, 文件名)
9)获取文件的扩展名(后缀)
os.path.splitext()
二 math和random模块
import math,cmath
1 数学模块 math、cmath(针对复数的数学模块)
1)浮点数转整数
math.ceil(浮点数) - 取较大整数
math.floor(浮点数) - 取较小整数
round(浮点数) - 四舍五入
print(math.ceil(2.8799)) # 3
print(math.ceil(2.1001)) # 3
print(math.floor(2.8799)) # 2
print(math.floor(2.1001)) # 2
print(round(2.6799)) # 3
print(round(2.4001)) # 2
2)求绝对值
math.fabs(数字) - 获取绝对值,结果是浮点数
abs(数据) - 获取绝对值,结束的类型和指定数据的类型一样
print(math.fabs(-29)) # 29.0
print(abs(-29)) # 29
print(abs(-23.56)) # 23.56
2 随机模块 - random
1)随机数
随机整数:random.randint(M, N) - 产生M到N的随机整数
随机小数:random.random() - 产生0到1的随机小数
random.randrange(M, N, step) - 在指定的等差数列中随机获取一个数
2)洗牌 import random
random.shuffle(序列) - 洗牌(随机打乱序列中元素的顺序; 序列必须是可变的并且有序的)
3)抽牌
random.choice(序列) - 从序列中随机获取一个元素
random.choices(序列, k=个数) - 从序列中随机获取指定个数个元素(默认情况下元素的权重都一样;有放回抽取)
random.choices(序列, 权重值序列, k=个数)
random.sample(序列, k=个数) - 从序列中随机获取指定个数个元素(无放回抽取)
random.sample(序列, k=个数, counts=权重序列) - Python3.9.0以后才可以使用
三 time和datetime模块
import time*
from datetime import datetime,date
1 time
1 )time.time() 获取当前时间,返回时间戳
时间戳 - 用指定时间到1970年1月1日0时0分0秒(格林威治时间)的时间差来表示一个时间,单位是秒
t1 = time.time() # 1646294602.6372879
print(t1)
2) time.localtime()
time.localtime() - 获取本地当前时间,返回结构体时间
time.localtime(时间戳) - 将时间戳转换成本地时间对应的结构体时间
t2 = time.localtime()
print(t2)
t3 = time.localtime(0)
print(t3)
t4 = time.localtime(1646294602.6372879)
print(t4, t4.tm_year)
3) time.mktime(结构体时间) 将结构体时间转换成时间戳
t5 = time.mktime(t4)
print(t5) # 1646294602.0
4 )time.strftime(时间格式字符串, 结构体时间) - 将结构体时间转换成指定格式的字符串时间
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale’s abbreviated weekday name.
%A Locale’s full weekday name.
%b Locale’s abbreviated month name.
%B Locale’s full month name.
%c Locale’s appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale’s equivalent of either AM or PM.
t_str1 = f’{t4.tm_year}年{t4.tm_mon}月{t4.tm_mday}日 {t4.tm_hour}:{t4.tm_min}:{t4.tm_sec}’
print(t_str1)
t_str2 = time.strftime(’%Y年%m月%d日 %H:%M:%S’, t4)
print(t_str2)
5 )time.strptime(字符串时间, 时间格式) - 将字符串时间转换成结构体时间
t1 = ‘1992-3-4’
result = time.strptime(t1, ‘%Y-%m-%d’)
print(f’周{result.tm_wday + 1}’)
6) time.sleep(数字) - 让程序暂停指定时间(秒)
time.sleep(3)
2 datetime 类
1)获取当前时间
datetime.now()
datetime.today()
t1 = datetime.now()
print(t1, type(t1))
t2 = datetime.today()
print(t2)
2)获取具体的时间信息
t1 = datetime.now()
print(t1.year)
print(t1.month)
print(t1.day)
print(t1.hour)
print(t1.minute)
print(t1.second)
print(t1.weekday())
3)字符串时间转datetime
datetime.strptime(字符串时间, 时间格式)
result = datetime.strptime(‘2011年2月4日’, ‘%Y年%m月%d日’)
print(result, result.weekday())
4)将datetime转化成字符串
datetime时间对象.strftime(时间格式)
result = t1.strftime(’%Y/%m/%d %a’)
print(result) # ‘2022/03/03 Thu’
5)将datetime转换成结构体时间
时间对象.timetuple()
result = t1.timetuple()
print(result)
print(time.mktime(result))
6)timedelta - 完成时间的加减操作
注意:timedelta在完成时间加减操作的时候时间只能以秒、分、时、周或者天为单位
时间对象 +/- timedelta()
t2 = datetime.strptime(‘2022-12-31 23:59:40’, ‘%Y-%m-%d %H:%M:%S’)
1个小时以后的时间
result = t2 + timedelta(hours=1)
print(result) # 2023-01-01 00:59:40
1个小时前的时间
result = t2 - timedelta(hours=1)
print(result) # 2022-12-31 22:59:40
3天后
result = t2 + timedelta(days=3)
print(result)
1个小时30分钟前
result = t2 - timedelta(hours=1, minutes=30)
print(result) # 2022-12-31 22:29:40