1

I'm currently doing an assignment that requires me to use CSV files in Python. I have the .csv file in the same directory as the program itself, yet it gives me the same "No such file or directory" error.

I am sure the name is not a problem. This problem has happened to me before with another file and I wasn't able to fix that either.

Code:

with open('file.csv') as file:
        content = csv.reader(file, delimiter=',')

FileNotFoundError: [Errno 2] No such file or directory: 'file.csv'
gavintc
  • 11
  • 2

1 Answers1

0

I fixed this by using the OS library for python. Change the current working directory with:

os.chdir(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))

This changes Python's working directory to the one that your file is contained in.

gavintc
  • 11
  • 2