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: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap