python常用的标准库

操作系统相关 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)
# 请求头

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值