FileNotFoundError:[错误2]我不知道为什么我的文件路径不存在

时间:2019-10-07 16:49:12

标签: python tensorflow

我正在使用python在tensorflow谷歌colab笔记本中编写一些代码。

我被这个错误困扰

  

FileNotFoundError:[错误2]文件b'/ home / brandon / Desktop / AnomalyDetection / Code / train / Y_10KHz_left.csv不存在: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

2 个答案:

答案 0 :(得分:1)

在尝试读取文件之前,应检查文件是否存在。

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}")

答案 1 :(得分:0)

尝试

 import os

    #load the data from local file into a dataframe
    file_path = '/home/brandon/Desktop/AnomalyDetection/Code/train/Y_10KHz_left.csv'

    if file_full_path:
         file_path, file_name = os.path.split(file_full_path)
         print(file_path, file_name)
        try:
             data = pd.read_csv(os.path.join(file_path, file_name))
             print(data)
         except Exception as e:
             print(f"error: {e}")