自动化学习笔记-python标准库
一、os模块
1、创建文件夹 path = “./test01/test02”
1)os.makedirs(path) 递归创建,test01不存在时,会创建test01文件夹
2)os.mkdir(path) 当前层级创建,test01不存在时,会报错
2、列出当前目录下存在的文件、目录
1)os.list("./")
3、删除文件夹、删除文件
1)os.removedirs("./test") 删除文件夹
2)os.remove("./test.py") 删除文件
4、获取当前路径
1)os.getcwd() 返回绝对路径
5、判断文件夹\文件是否存在
1)os.path.exists()
2)存在返回True 不存在返回False
二、time模块
1、时间格式
1)time.asctime() 国外的时间格式
2)time.time() 当前时间的时间戳
3)time.sleep() 等待
4)time.localtime() 将当前时间戳转为时间元组
5)time.strftime(“%Y%m%d %H%M%S ”,time.localtime())将时间元组转为带格式的时间
6)如以下示例:
转化为年月日时分秒格式
now_time = time.localtime()
now_time2 = time.strftime("%Y{}%m{}%d{} %H{}%M{}%S",now_time)
print(now_time2.format("年","月","日",":",":"))
三、urllib模块、request模块
1、urllib模块使用
1)import urllib.request 导入模块
2)r = urllib.request.urlopen(url)
3)print(r.status) 返回响应200
4)print(r.read()) 返回响应内容
5)print(r.headers) 返回响应头
2、request模块使用
1)import request
2)r = request.get(url)
3)print(r.status_code) 返回响应200
4)print(r.text) 返回响应内容
5)print(r.headers) 返回响应头
四、math模块
1、常用函数
1)math.ceil() 向上取整
2)math.floor() 向下取整
3)math.sqrt(4) 开平方,返回2.0(浮点数)