FileNotFoundError:[Errno 2]我不明白为什么我的文件路径不存在

2024-09-30 14:34:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python在一个tensorflow google colab笔记本上写了一些代码。你知道吗

我被这个错误困住了

FileNotFoundError: [Errno 2] File b'/home/brandon/Desktop/AnomalyDetection/Code/train/Y_10KHz_left.csv' does not exist: b'/home/brandon/Desktop/AnomalyDetection/Code/train/Y_10KHz_left.csv'

下面是有问题的代码片段:

from __future__ import absolute_import, division, print_function, unicode_literals
import functools

import numpy as np
import tensorflow as tf
import os
import pandas as pd
import matplotlib.pyplot as plt
import sklearn

#load the data from local file into a dataframe
path = '/home/brandon/Desktop/AnomalyDetection/Code/train/Y_10KHz_left.csv'
df = pd.read_csv(path)
df.head()

为了确认我的路是对的

(base) brandon@brandon:~/Desktop/AnomalyDetection/Code/train$ find $PWD -type f | grep "Y_10KHz_left.csv"
/home/brandon/Desktop/AnomalyDetection/Code/train/Y_10KHz_left.csv

Tags: csvpath代码fromimporthometensorflowas
1条回答
网友
1楼 · 发布于 2024-09-30 14:34:34

在尝试读取文件之前,应该先检查文件是否存在。你知道吗

import os

#load the data from local file into a dataframe
file_path = '/home/brandon/Desktop/AnomalyDetection/Code/train/Y_10KHz_left.csv'
if os.path.exists(file_path):
    df = pd.read_csv(file_path)
    df.head()
else:
    print(f"Unable to find the file at {file_path}")

相关问题 更多 >