Python常用的标准库
Python标准库常见模块
操作系统相关:os
os模块主要是对文件,目录的操作
os模块常用方法:
os.mkdir()创建目录
os.removedirs()删除目录
os.getcwd()获取当前目录
os.path.exists(dir or file)判断文件或目录是否存在(返回一个布尔型)
练习:——创建一个文件夹,并在该文件夹下创建一个文件,进行编辑
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,datetime
获取当前时间以及时间格式经的模块
导入方法:import time
time模块常用的方法:
time.asctime()国外的时间格式Thu Apr 30 15:19:25 2020
time.time()时间戳1588231165.5953197
time.sleep()等待
time.localtime()时间戳转成时间元组——若没给参数,则返回当前时间的元组;也可以给时间秒数(比如time.time那个时间戳)time.struct_time(tm_year=2020, tm_mon=4, tm_mday=30, tm_hour=15, tm_min=19, tm_sec=25, tm_wday=3, tm_yday=121, tm_isdst=0)
time.strftime()将当前时间戳转成带格式的时间(这个格式是可以自定义的)
格式:time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime())
2020-04-30 15:23:22
练习——获取两天前的时间
now_timestamp = time.time() two_day_before = now_timestamp - 60*60*24*2 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(two_day_before)))
科学计算:math
math.ceil(x)返回大于等于参数x的最小整数
math.floor(x)返回小于等于参数x的最大整数
math.sqrt(x)平方根
网络请求:urllib(urllib.request)
导入方法:
import urllib.request response=urllib.request.urlopen('http://www.baidu.com')