YOLOv8的Python基础--函数篇

1. 文件/目录操作相关函数

这些函数来自 osshutil 模块:

函数/用法 作用 示例 说明
os.listdir(dir) 列出目录下所有文件名 os.listdir("./images") 返回文件名列表(不包含路径)
os.path.join() 拼接路径 os.path.join("dir", "file.txt") 跨平台安全拼接路径(Windows/Linux兼容)
os.path.splitext() 分割文件名和扩展名 name, ext = os.path.splitext("img.jpg") 返回 ("img", ".jpg")
os.path.exists() 检查文件/目录是否存在 os.path.exists("label.txt") 返回 True/False
os.makedirs() 递归创建目录 os.makedirs("dir/subdir", exist_ok=True) exist_ok=True 避免目录已存在时报错
os.path.basename() 获取路径中的文件名 os.path.basename("/data/img.jpg") 返回 "img.jpg"
shutil.copy(src, dst) 复制文件 shutil.copy("a.txt", "backup/a.txt") 如果目标目录不存在会报错

2. 数据处理相关函数

函数/用法 作用 示例 说明
random.random() 生成0-1之间的随机数 if random.random() < 0.2: 用于随机分配数据集
range(len()) + tqdm() 带进度条的循环 for i in tqdm(range(100)): 显示处理进度(来自tqdm库)

3. 代码中未直接使用但导入的库

虽然导入但未实际使用的库及其典型用途:

  • cv2 (OpenCV):图像读取/处理(如 cv2.imread()

  • matplotlib.pyplot (plt):数据可视化(如 plt.plot()

  • numpy (np):数值计算(如 np.array()

相关推荐
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程1 天前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪1 天前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook1 天前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田2 天前
使用 pkgutil 实现动态插件系统
python
前端付豪2 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽2 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战2 天前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋2 天前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python