I'm trying to read in a csv file in pycharm that I downloaded on my mac and when I run my code it says no such file exists

272 views Asked by At
#import necessary modules
import csv
with open ("C:/iCloud Drive/Desktop/Python/o.csv") as f:
  data = csv.reader(f)
  for row in data:
        print(row)


Heres the error message

Traceback (most recent call last):
  File "/Users/noahhenninger/PycharmProjects/Refactoring/main.py", line 3, in <module>
    with open ("C:/iCloud Drive/Desktop/Python/o.csv") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:/iCloud Drive/Desktop/Python/o.csv'

I tried adding more specific info about the location of the file but that didn't seem to work.

3

There are 3 answers

0
C.Nivs On

Since you are cross-platform (windows vs unix), it might be best to use pathlib.Path.home to address both operating systems:

from pathlib import Path
import csv

path = Path.home() / 'Desktop' / 'Python' / 'o.csv'

with open(path) as f:
  data = csv.reader(f)

  for row in data:
        print(row)
0
Biny Kan On

The code you provided, there doesn't seem to be any syntax errors. However, the error message you received. Make sure that the path you specified is correct and the file is located at that location.

Make sure that you have spelled the filename and path correctly, including capitalization. Check that you have the necessary permissions to access the file. If the file is located in a restricted folder or on a network drive, you may need to get permissions from the system administrator.

0
Mika On

Maybe it has to do with icloud saving your data in cloud?! I just moved the file to another folder and it worked. iCloud will update later today so tomorrow I expect a similar error.