十、Python常用标准库

Python标准库常见模块

  • 操作系统相关:os
  • 时间与日期:time、datetime
  • 科学计算:math
  • 网络请求:urllib
  • ... ...

os模块

  • os模块主要是对文件、目录的操作
  • 常用方法:
    • os.mkdir() 创建目录
    • os.removedirs() 删除文件
    • os.getcwd() 获取当前目录
    • os.path.exists(dir or file) 判断文件或目录是否存在
import os

os.mkdir("testdir")     #在当前路径创建目录
print(os.listdir("./")) # 打印当前目录的文件
os.removedirs("testdir")# 删除文件
print(os.getcwd())      # 打印当前路径
  • 练习:创建b/test.txt,即在当前路径创建目录b并在其下创建文件test.txt
import os

# 练习:创建b/test.txt,即在当前路径创建目录b并在其下创建文件test.txt
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, os using")

运行结果:


image.png

time模块

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

  • 导入方式:import time

  • 常用方法:

    • time.asctime() 国外的时间格式
    • time.time() 时间戳
    • time.sleep() 等待
    • time.localtime() 时间戳转换为时间元组
    • time.strftime() 将当前的时间戳转成带格式的时间
      • 格式:time.strftime("%Y-%m-%d %H:%M:%S, time.localtime()")
  • strftime()的格式表示:

    %Y Year with century as a decimal number.
    %m Month as a decimal number [01,12].
    %d Day of the month as a decimal number [01,31].
    %H Hour (24-hour clock) as a decimal number [00,23].
    %M Minute as a decimal number [00,59].
    %S Second as a decimal number [00,61].
    %z Time zone offset from UTC.
    %a Locale's abbreviated weekday name.
    %A Locale's full weekday name.
    %b Locale's abbreviated month name.
    %B Locale's full month name.
    %c Locale's appropriate date and time representation.
    %I Hour (12-hour clock) as a decimal number [01,12].
    %p Locale's equivalent of either AM or PM.

import time

print(time.asctime())
print(time.localtime())
print(time.time())
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

运行结果:

Mon Mar 29 22:27:10 2021
time.struct_time(tm_year=2021, tm_mon=3, tm_mday=29, tm_hour=22, tm_min=27, tm_sec=10, tm_wday=0, tm_yday=88, tm_isdst=0)
1617028030.2485793
2021-03-29 22:27:10
  • 练习:获取两天前的时间
impor time

# 获取两天前的时间
now_time = time.time()
two_day_ago = now_time - 60*60*24*2
time_tuple = time.localtime(two_day_ago) # 将时间戳转换为时间元组
print(time.strftime("%Y年%m月%d日 %H:%M:%S", time_tuple))

运行结果:2021年03月27日 22:32:57

urllib库

建议使用Requests第三方库

import urllib.request

response = urllib.request.urlopen("http://www.baidu.com")
print(response.status) # 返回状态码
print(response.read())  # 响应信息
print(response.heanders)    # 头部信息

math库

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

print(math.ceil(3.19)) #结果为4
print(math.floor(3.19)) #结果为3
print(math.sqrt(9)) #结果为3

下一节:Python多线程处理,包括进程与多线程处理、log处理。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Python中几种常用的模块 1. os模块:这个是调用操作系统的方法,使用方法为:import os 查看系统的...
    言言老师阅读 288评论 0 1
  • 一,什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的...
    100斤的瘦子_汤勇阅读 498评论 0 0
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,932评论 0 13
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,259评论 0 38
  • import timeprint(time.time()) # 时间戳:1487130156.419527prin...
    功夫杨阅读 229评论 0 0

友情链接更多精彩内容