常用模块和文件操作
os模块
1.os模块的作用
提供和文件或者文件夹(目录)或者路径相关操作
例如:创建文件夹、创建文件、删除文件、判断文件是否存在、获取绝对路径、获取一个文件夹中所有的文件等等
1)返回当前工作目录
import os
print(os.getcwd())
2)获取指定文件夹中所有内容的文件
import os
print(os.listdir(r'路径'))
3)创建文件夹
#在指定路径下创建指定的文件夹(要求整个路径中只有最后哪个文件夹不存在才可以创建)
os.mkdir(文件夹路径)
#递归创建文件夹(可以创建所有路径中不存在的路径值
os.makedirs(文件夹路径)
4)返回绝对路径
返回相对路径的绝对路径
os.path.abspath(相对路径)
绝对路径:文件或者文件夹在计算机中的全路径(windows电脑绝对路径从盘开始写)
相对路径:用.表示当前目录(当前目录值的是当前代码文件所在的文件夹)、用…表示当前目录的上层目录。
5)获取文件名
#获取文件路径中的文件名
result = os.path.basename('文件路径')
print(result)
6)检测路径是否有效
print(os.path.exists('./demo.py'))
7)判断是否是文件或者文件夹
#判断是否是文件
os.path.isfile(路径)
#判断是否是文件夹
os.path.isdir(路径)
8)把目录和文件名合成一个路径
url = './files'
name = 'aaa.txt'
result = os.path.join(url,name)
print(result)
9)获取文件的扩展名(后缀)
url1 = '文件路径'
result = os.path.splitext(url1)
print(result[-1])
math和random模块
1.数学模块
cmath:针对复数的数学模块
math、cmath
impoty math,cmath
1)浮点数转整数
#取较大整数
math.ceil(浮点数)
#取较小整数
math.floor(浮点数)
#四舍五入
round(2.4001)
2)求绝对值
#获取绝对值,结果是浮点数
math.fabs(数字)
#获取绝对值,结果的类型和指定数据的类型一样
abs(数据)
2.随机模块
#产生M-N随机整数
mport random
print(random.randint(M,N))
#产生0-1随机小数
print(random.random(),random.random()*5 + 10)
print(random.uniform(10,20))
#在指定的等差数列中随机获取一个数
print(random.randrange(M,N,step))
#random.shuffle(序列) - 洗牌(随机打乱列表中元素的顺序:序列必须是可变的并且有序)
nums = [10,20,30,40,50,60]
random.shuffle(nums)
print(nums)
#random.choice(序列) - -从序列中随机抽取一个元素
#random.choices(序列,key=个数) - 从序列中随机抽取key个元素(默认情况下元素的权重都一样;放回抽取)
random.choices(序列,权重,k=个数)
import random
options = ['特等奖','一等奖','二等奖','三等奖','再接再励']
result = random.choices(options,[1,3,10,100,100],k=1)
print(result)
random.sample(序列,key=个数)
从序列中随机抽取key个元素(无放回抽取)
random.sample(序列,k=个数,counts=权重序列) (3.9版本以后才可以使用)
import random
options = ['特等奖','一等奖','二等奖','三等奖','再接再励']
result = random.sample(options,k=1,count=[1,10,20,30,40])
print(result)
time模块
import time
1.time.time()
获取当前时间,返回时间戳
t1 = time.time()
时间戳:用指定时间到1970年1月1日0时0分0秒(格林威治时间)的时间差来表示一个时间,单位是秒
2.time.localtime()
#获取本地当前时间,返回结构体时间
time.localtime()
#将时间戳转换成本地时间对应的结构体时间
t2 = time.localtime(时间戳)
3.time.mktime()
time.kmtime(结构体时间):将结构体时间转换成时间戳
t4 = time.mktime(t2)
4.time.strftime()
time.strftime(时间个数字符串,结构体时间):将结构提时间转换成指定格式的字符串时间
str = time.strftime('%Y年%m月%d日 %H:%M:%S',t2)
%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.
5.time.strptime()
time.strptime(字符串时间,时间格式):将字符串时间转换成结构体时间
6.time.sleep()
time.sleep(秒):让程序暂停指定时间
time.sleep(3)
datetime模块
1.datetime类
时间对象、字符串
1)获取当前时间
t1 = datetime.now()
t2 = datetime.today()
2)获取具体的时间信息
print(t1.year)
print(t1.month)
print(t1.day)
print(t1.hour)
print(t1.minute)
print(t1.second)
print(t1.weekday())
3)字符串时间转datetime
result = datetime.strptime('2011年2月4日','%Y年%m月%d日')
print(result,result.weekday())
4) 将datetime转化成字符串
datetime时间对象.strftime(时间格式)
datetime.strptime(字符串,时间格式)
result = t1.strftime('%Y/%m/%d %a')
print(result)
5)将datetime转化成结构体时间
时间对象.timetuple()
import time
result = t1.timetuple()
print(result)
print(time.mktime(result))
2.timedelta
完成时间的加减操作
注意:timedelta在完成时间加减操作的时候只能以秒、分、是、周、或者天为单位
from datetime import datetime,timedelta
t2 = datetime.strptime('2022-12-31 23:59:40','%Y-%m-%d %H:%M:%S')
#1个小时后的时间
result = t2 + timedelta(hours=1)
print(result)
#1个小时前的时间
result = t2 - timedelta(hours=1)
print(result)
#3天后
result = t2 + timedelta(days=3)
print(result)
#1小时30分子前
result = t2 - timedelta(hours=1,minutes=30)
print(result)
#3周前
result = t2 - timedelta(weeks=3)
print(result)