Python error saying that the file does not exist, but it does exist

Asked

Viewed 189 times

0

Code:

import csv
import random

dataset = []

with open('data.csv') as _file:
    data = csv.reader(_file,delimiter=',')
    for line in data:
        line = [float(elemento) for elemento in line]
        dataset.append(line)

Error:

FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'

Archives:

inserir a descrição da imagem aqui

1 answer

3


Hello,

It turns out that you are running your python file a folder before RD2, which ends up defining the project root as the folder containing the folder RD2. (I believe it’s running on Vscode)

So when you use the with open('data.csv'), it looks for the file at the root. And, as it doesn’t really exist in that folder, it will give the error.

If you play the data.csv out of RD2, the execution will take place normally. Or you put with open('RD2/data.csv') as _file: which will also work! :)

  • Thank you, now it’s working

  • You’re welcome, @Brunoluizbender :)

  • If possible, mark the answer as a solution for others to see that the question has already been answered :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.