操作系统相关 os
"""
os模块主要是对文件或者目录的操作
常用方法:
os.mkdir() 创建目录
os.removedirs() 删除文件
os.getcwd() 获取当前目录
os.path.exists(dir or flie) 判断文件或者目录是否存在
"""
import os
# os.mkdir("testdir")
print(os.listdir("./")) # 当前的目录和文件
# os.removedirs("testdir") # 删除文件
print(os.getcwd()) # 打印当前的绝对路径
print(os.path.exists("b"))
# False 当前文件不存在
if not os.path.exists("b"):
os.mkdir("b")
if not os.path.exists("b/test.txt"):
with open("b/test.txt", "w") as f:
f.write("hello")
时间与日期 time datetime
"""
time模块
获取当前时间以及时间格式的模块
time.asctime() 国外的时间格式
time.time() 时间戳
time.sleep() 等待
time.localtime() 时间戳转换为时间元组
time.strftime() 将当前时间戳转换成带格式的时间
格式time.strftime("%Y-%m-%d %H-%M-%S",time.localtime())
"""
import time
print(time.asctime())
# Sun Oct 24 20:43:25 2021
print(time.time()) # 从纪元开始的秒数,唯一
# 1635079425.0288339
print(time.localtime()) # 生成的元组的形式
# time.struct_time(tm_year=2021, tm_mon=10, tm_mday=24, tm_hour=20, tm_min=47, tm_sec=42, tm_wday=6, tm_yday=297, tm_isdst=0)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# 2021-10-24 20:53:28
# 获取两天前的时间
now_time = time.time()
two_day_before = now_time - 60*60*24*2
time_tuple = time.localtime(two_day_before)
print(time.strftime("%Y-%m-%d %H:%M:%S", time_tuple))
科学计算 math
"""
math库
math.ceil() 返回大于等于参数x的最小整数
math.floor(x) 返回小于等于参数x的最大整数
math.sqrt(x) 平方根
"""
import math
print(math.ceil(6.6))
# 7
print(math.floor(6.6))
# 6
print(math.sqrt(9))
# 3.0
网络请求 urllib
"""
urllib库
"""
import json
import urllib.request
response: object = urllib.request.urlopen('https://www.baidu.com/')
print(response.status)
# 200
print(response.read())
# url的返回
print(response.headers)
# 请求头