Python标准库常见模块:
操作系统相关:OS
时间与日期:time,datetime
科学计算:math
网络请求:urllib
OS模块
os模块主要是针对文件,目录的操作
常用方法:
os.mkdir() 创建目录
os.removedirs() 删除文件
os.getcwd() 获取当前目录
os.path.exists(dir or file)判断文件或者目录是否存在
import os
#os.makedirs("testdir") #创建目录
#打印有哪些文件
print(os.listdir("./"))
# os.removedirs("testdir") #删除目录
print(os.getcwd()) #打印当前路径
print(os.path.exists("b"))
if not os.path.exists("b"):
os.mkdir("b")
if not os.path.exists("b/test.txt"):
f=open("b/test.txt","w")
f.write("hello,os using")
f.close()
time 模块
获取时间以及时间格式的模块
import time
time常用的方法:
import time
print(time.asctime()) #国外时间格式
print(time.time()) #时间戳
print(time.localtime()) #时间格式转换为元组
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())) #转换时间格式
#获取两天前的时间
two_befor_time=time.time()-60*60*24*2
time_tumple=time.localtime(two_befor_time)
print(time.strftime("%Y-%m-%d %H:%M:%S", time_tumple))
urllib库
math库
import math print(math.ceil(5.5)) #大于或等于这个最小整数 print(math.floor(5.5)) #小于这个值的整数 print(math.sqrt(9)) #开平方



