FileNotFoundError: [Errno 2] No such file or directory in Python

Dung Do Tien Aug 15 2021 237

I have created a project in Python 3.8.2 and I want to read data from a csv file as below:

import csv

with open('student.csv','r') as file:
    student = csv.reader(file)

print(student)  

When running the app I get an error throw FileNotFoundError: [Errno 2] No such file or directory

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    with open('student.csv','r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'student.csv'

And below is my struct file:

ReadFileExample
└── app
    ├── main.py 
    └── student.csv

I don't know why my file is not found??

Please help me if you know any reason.

Have 2 answer(s) found.
  • D

    Denis Cano Aug 15 2021

    Might be you are in the wrong folder. You can get the exact working folder as below:

    import os
    # get current working directory
    cwd = os.getcwd()
    print(cwd)
    
    #get files in directory
    files = os.listdir(cwd) 
    
    print(files)

    It helps you get an absolute path file. I hope it is useful for you.

  • K

    Kingtub Kakkak Aug 15 2021

    Why do you not try using the absolute path file as below:

    import csv
    with open('C:\\your_path\\student.csv', 'r') as f:
    
Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close