python常用标准库

文章目录

os

操作系统相关:

主要针对文件、目录的操作

常用方法:

方法方法的作用
os.listdir()列出当前目录下包含的文件和目录
os.mkdir()创建目录
os.removedirs()删除文件或目录
os.getcwd()获取当前目录
os.path.exists(dir or file)判断文件或目录是否存在
import os

# 如果不存在b目录,则创建b目录
if not os.path.exists("b"):
	os.mkdir("b")

# 如果在b目录下不存在test.txt文件,则创建文件并写入数据
if not os.path.exists("b/test.txt"):
	with open("b/test.txt","w") as f:
		f.write("hello, os")
	




time

获取当前时间以及时间格式的模块

方法方法的作用
time.asctime()获取国外格式的时间
time.time()时间戳
time.sleep()等待
time.localtime()将 时间戳 转成 时间元组
time.strftime()将时间元组转换成带格式的时间
import time

time.asctime()	# Thu May 13 15:27:45 2021

time.time() # 1620890865.2453315

time.localtime() 
# time.struct_time(tm_year=2021, tm_mon=5, tm_mday=13, tm_hour=15, tm_min=27, tm_sec=45, tm_wday=3, tm_yday=133, tm_isdst=0)

time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 2021-05-13 15:27:45

time.localtime() 中可传入设定时间的时间戳 从而生成该时间的 时间元组

'''求当前时间的三天前的时间,以yyyy-MM-dd HH:mm:ss格式输出'''
import time

now_timestamp = time.time()
threeDayAge_timestamp = now_timestamp - 3 * 24 * 60 * 60
time_tuple = time.localtime(threeDayAge_timestamp)
print(time.strftime("%Y-%m-%d %H:%M:%S", time_tuple))




urllib

python 2

  • import urllib2
  • response = urllib2.urlopen(“http://www.baidu.com”)

python 3

  • import urllib.request
  • response = urllib.request.urlopen(‘http://www.baidu.com’)
print(response.status)
print(response.read())
print(response.headers)




math

math.ceil(x) 返回大于等于参数x的最小整数
math.floor(x) 返回小于等于参数x的最大整数
math.sqrt(x) 平方根

import math

print(math.ceil(5.3))
print(math.floor(5.6))
print(math.sqrt(49))

'''
6
5
7.0
'''




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值