常见模块
os模块
作用:用于和
操作系统
进行交互 通用操作:
- 获取平台信息
- 对目录的操作
- 判断操作
# os模块:用于和操作系统交互
# os模块提供了很多方法用于处理文件和目录
import os
#1 os.name 指示你正在使用的平台 windows返回'nt'; Linux返回'posix'
print(os.name) #nt
#2 os.getenv() 获取环境变量
print(os.getenv('PATH')) #返回环境变量的值
#3 os.path.abspath(path) 返回path规范化的绝对路径
print(os.path.abspath('.')) #返回当前目录的绝对路径
#4 os.path.split(path) 将path分割成目录和文件名二元组返回
print(os.path.split('/Users/michael/testdir/file.txt')) #('/Users/michael/testdir', 'file.txt')
#5 os.path.splitext(path) 分离文件名与扩展名
print(os.path.splitext('/path/to/file.txt')) #('/path/to/file', '.txt')
# os.path.basename(path) 返回文件名
print(os.path.basename('/path/to/file.txt')) #file.txt
#6 os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
print(os.path.join('/Users/michael', 'testdir', 'file.txt')) #/Users/michael/testdir/file.txt
#7 os.path.isdir(path) 判断path是不是一个目录
# r''表示字符串不转义 否则会报错
print(os.path.isdir(r'D:\file\code\pythonProject\basicKnowledge')) #True
#8 os.path.isfile(path) 判断path是不是一个文件
print(os.path.isfile(r'D:\file\code\pythonProject\basicKnowledge')) #False
#9 os.listdir(path) 返回path目录下的所有文件 目录不存在则抛出异常
print(os.listdir(r'D:\file\code\pythonProject\basicKnowledge'))
#10 os.mkdir(path) 创建一个目录
os.mkdir(r'D:\file\code\pythonProject\basicKnowledge')
sys模块
# sys模块 负责程序与python解释器的交互
import sys
# 获取编码格式 sys.getdefaultencoding()
print("默认编码格式:",sys.getdefaultencoding()) # utf-8
# 获取系统版本 sys.version 3.13.2 (tags/v3.13.2:4f8bb39, Feb 4 2025, 15:23:48) [MSC v.1942 64 bit (AMD64)]
print("系统版本:",sys.version)
# 获取系统平台 sys.platform 返回值:win32 win64 linux
print("系统平台:",sys.platform) # win32
# 获取环境变量 sys.path 返回一个列表 第一项是当前文件所在目录
print("环境变量:",sys.path)
time模块
# time模块
# 时间的三种表示方式
# 1.时间戳 2.格式化字符串 3.元组
import time
# time.time() 返回当前时间的时间戳
print(time.time())
# time.strftime() 返回当前时间的格式化字符串
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
# time.localtime() 返回当前时间的元组
print(time.localtime())
格式之间的相互转换
# time模块
# 时间的三种表示方式
# 1.时间戳 2.格式化字符串 3.元组
import time
# 时间戳转换为另外两种表示方式
# time.time() 返回当前时间的时间戳
timestamp = time.time()
# time.strftime() 返回当前时间的格式化字符串
timeFormat = time.localtime(timestamp)
# time.localtime() 返回当前时间的元组
timeTuple = time.localtime(timestamp)
print("时间戳:", timestamp)
print("格式化字符串:", timeFormat)
print("元组:", timeTuple)
# 格式化字符串转换为另外两种表示方式
# time.strptime() 返回格式化字符串的元组
timeStr = "2021-01-01 00:00:00"
timeTuple = time.strptime(timeStr, "%Y-%m-%d %H:%M:%S")
# time.mktime() 返回格式化字符串的时间戳
timestamp = time.mktime(timeTuple)
print("格式化字符串:", timeStr)
print("元组:", timeTuple)
print("时间戳:", timestamp)
# 元组转换为另外两种表示方式
# time.mktime() 返回元组的时间戳
timestamp = time.mktime(timeTuple)
# time.strftime() 返回元组的格式化字符串
timeFormat = time.strftime("%Y-%m-%d %H:%M:%S", timeTuple)
print("元组:", timeTuple)
print("时间戳:", timestamp)
print("格式化字符串:", timeFormat)
logging模块
# logging 模块 用于输出运行日志
# logging 模块定义了以下函数
# 常见的等级有以下几种:
#NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL
# NOTSET 默认级别,输出所有的日志
# DEBUG 详细信息,通常只有在诊断问题时才会关心这些信息
# INFO 证明事情按预期工作
# WARNING 表明发生了一些意外,或者不久的将来会发生问题(例如‘磁盘空间不足’)。软件仍在正常工作
# ERROR 由于更严重的问题,软件已不能执行一些功能
# CRITICAL 严重错误,表明软件已不能继续运行
import logging
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")
logging.critical("critical message")
设置日志输出级别
将日志级别设置到最小,就可以得到
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")
logging.critical("critical message")
设置日志格式
import logging
#设置日志格式
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")
logging.critical("critical message")
设置存储路径
import logging
#设置日志格式和存储路径
# level 日志级别
# format 日志格式
# filename 日志文件名
# filemode 写入模式 a 追加 w 覆盖
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',filename='test.log',filemode='a')
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")
logging.critical("critical message")
random模块
# random 模块
import random
# 生成一个随机整数
print(random.randint(1, 10))
# 生成一个随机小数
print(random.uniform(1.1, 5.4))
# 按照指定的步长生成一个随机数
print(random.randrange(1, 10, 2)) # 1-10之间的奇数 1,3,5,7,9 2是步长