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


Python Code:
import os

file_path = "/User/c2c/Desktop/raw_data.csv"

with open(file_path, "r") as file:
    for line in file:
        print(line.strip())
Error:
---------------------------------------------------------------------------
FileNotFoundError

Traceback (most recent call last)
<ipython-input-7-77369353dc39> in <cell line: 5>()
      3 file_path = "/User/c2c/Desktop/raw_data.csv"
      4 
----> 5 with open(file_path, "r") as file:
      6     for line in file:
      7         print(line.strip())

FileNotFoundError: [Errno 2] No such file or directory: '/User/c2c/Desktop/raw_data.csv'
FileNotFoundError- [Errno 2] No such file or directory - Python
FileNotFoundError- [Errno 2] No such file or directory - Terminal

Reason for FileNotFoundError Exception

    The reason for the FileNotFoundError exception in Python is that the file or directory that you are trying to open does not exist.


How to fix FileNotFoundError Exception

    Below are some area's that you should investigate to fix this issue.

    • Is the file name specified correctly?
    • Is the extension (.txt, .csv, .xlsx, etc)of the file correct?
    • Is the path of the file specified correctly?
    • File name and path are case sensitive, so do they match?
    • Do you have permission to read the file?
    • If reading the file from a remote location, make sure there are no network issues.

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap