D:\shijin\venv\pythonProject\Scripts\python.exe C:\Users\user\yolov5\2025-06\1.py Traceback (most recent call last): File "C:\Users\user\yolov5\2025-06\1.py", line 36, in <module> split_dataset('path_to_source_dataset', 'path_to_target_dataset') File "C:\Users\user\yolov5\2025-06\1.py", line 7, in split_dataset images = [f for f in os.listdir(os.path.join(source_dir, 'images')) if f.endswith('.jpg')] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'path_to_source_dataset\\images'
时间: 2025-06-13 07:57:44 浏览: 14
### 解决Python脚本中因路径错误导致的FileNotFoundError问题
在处理YOLOv5自定义数据集时,路径配置和文件操作是关键步骤。以下是对`split_dataset`函数、`os.listdir`方法以及YOLOv5自定义数据集路径配置的详细解析。
#### 1. `os.listdir` 方法引发的 FileNotFoundError 问题
当使用`os.listdir`方法时,如果指定的路径不存在或路径格式不正确,将抛出`FileNotFoundError: [WinError 3] 系统找不到指定的路径`的错误[^2]。为避免此问题,建议:
- 使用绝对路径而非相对路径。例如,`D:\\pycharm\\RelearnPython\\relearn-class\\DateSet`。
- 在调用`os.listdir`之前,先验证路径是否存在。可以使用`os.path.exists`方法检查路径是否有效。
以下是改进后的代码示例:
```python
import os
def check_path(path):
if not os.path.exists(path):
raise FileNotFoundError(f"指定的路径 {path} 不存在。")
return os.listdir(path)
# 示例调用
image_dir = "D:\\pycharm\\RelearnPython\\relearn-class\\DateSet\\images"
check_path(image_dir)
```
#### 2. `split_dataset` 函数中的路径问题
在`split_dataset`函数中,如果源目录或目标目录路径不正确,同样会引发`FileNotFoundError`。确保所有路径都以绝对路径形式提供,并验证路径的有效性[^2]。
以下是改进后的`split_dataset`函数:
```python
import os
import random
import shutil
def split_dataset(source_dir, target_dir, train_ratio=0.8, val_ratio=0.1, test_ratio=0.1):
"""
自动划分数据集为训练集、验证集和测试集。
参数:
source_dir: 源数据集路径
target_dir: 目标数据集路径
train_ratio: 训练集比例
val_ratio: 验证集比例
test_ratio: 测试集比例
"""
# 验证路径有效性
if not os.path.exists(source_dir):
raise FileNotFoundError(f"源目录 {source_dir} 不存在。")
if not os.path.exists(os.path.join(source_dir, 'images')):
raise FileNotFoundError(f"源目录下的 images 文件夹 {os.path.join(source_dir, 'images')} 不存在。")
if not os.path.exists(os.path.join(source_dir, 'labels')):
raise FileNotFoundError(f"源目录下的 labels 文件夹 {os.path.join(source_dir, 'labels')} 不存在。")
images = [f for f in os.listdir(os.path.join(source_dir, 'images')) if f.endswith('.jpg')]
random.shuffle(images)
total_images = len(images)
train_split = int(total_images * train_ratio)
val_split = int(total_images * (train_ratio + val_ratio))
os.makedirs(os.path.join(target_dir, 'train', 'images'), exist_ok=True)
os.makedirs(os.path.join(target_dir, 'train', 'labels'), exist_ok=True)
os.makedirs(os.path.join(target_dir, 'val', 'images'), exist_ok=True)
os.makedirs(os.path.join(target_dir, 'val', 'labels'), exist_ok=True)
os.makedirs(os.path.join(target_dir, 'test', 'images'), exist_ok=True)
os.makedirs(os.path.join(target_dir, 'test', 'labels'), exist_ok=True)
for i, img in enumerate(images):
label = img.replace('.jpg', '.txt')
if i < train_split:
shutil.copy(os.path.join(source_dir, 'images', img), os.path.join(target_dir, 'train', 'images', img))
shutil.copy(os.path.join(source_dir, 'labels', label), os.path.join(target_dir, 'train', 'labels', label))
elif i < val_split:
shutil.copy(os.path.join(source_dir, 'images', img), os.path.join(target_dir, 'val', 'images', img))
shutil.copy(os.path.join(source_dir, 'labels', label), os.path.join(target_dir, 'val', 'labels', label))
else:
shutil.copy(os.path.join(source_dir, 'images', img), os.path.join(target_dir, 'test', 'images', img))
shutil.copy(os.path.join(source_dir, 'labels', label), os.path.join(target_dir, 'test', 'labels', label))
```
#### 3. YOLOv5 自定义数据集路径配置教程
在YOLOv5中,`data.yaml`文件用于配置数据集路径。如果路径配置错误,可能会导致模型无法加载数据集。以下是正确的路径配置方法:
```yaml
train: D:\pycharm\RelearnPython\relearn-class\DateSet\images\train # 训练集路径
val: D:\pycharm\RelearnPython\relearn-class\DateSet\images\val # 验证集路径
nc: 5 # 类别数量
names: ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips'] # 类别名称
```
确保路径使用反斜杠`\`分隔,并且路径指向实际存在的文件夹[^4]。
#### 4. 其他注意事项
- 如果在PyCharm中运行YOLOv5训练脚本,可能需要调整`batch-size`参数以避免内存不足的问题[^3]。
- 确保所有路径拼接操作使用`os.path.join`方法,以兼容不同操作系统。
---
阅读全文
相关推荐



