0

I am trying to read in a CSV file from my desktop:

My code is as follows:

import pandas as pd
import csv
from pathlib import Path

csv = r'C:\Users\nulli\OneDrive\Desktop\Work_Sample.csv'
df = pd.read_csv(csv, sep=',')

Error:

---> df = pd.read_csv(csv, sep=',')

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\nulli\\OneDrive\\Desktop\\Work_Sample.csv'
desertnaut
  • 57,590
  • 26
  • 140
  • 166
iceAtNight
  • 49
  • 2
  • 7
  • 2
    I think you have a typo in your path `C:Users\nulli\OneDrive\Desktop\Work_Sample.csv` should be `C:\Users\nulli\OneDrive\Desktop\Work_Sample.csv`. – ndclt Mar 05 '21 at 21:24
  • If you want to be entirely sure of the path, go to your file explorer, right click on your file > properties > location, and copy this. – sander Mar 05 '21 at 21:25
  • @ndclt ahh that is definitely part of the problem. I now get an error of csv = 'C:\Users\nulli\OneDrive\Desktop\Work_Sample.csv' ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape – iceAtNight Mar 05 '21 at 21:36

3 Answers3

0

You shouldn't use backslashes for paths as Python has some special escape characters like \n that means newline or \t that means a tab. The easiest way is to always use standard slashes. You can also use:

r"some_path\with\backslashes"

to ignore escape characters and treat all backslashes as backslashes.

But the best way is to use a package which was designed to handle paths. pathlib is a great tool for that:

from pathlib import Path
my_csv = Path("C:/Usersnulli/OneDrive/Desktop/Work_Sample.csv")
df = pd.read_csv(my_csv.resolve(), sep=',')

resolve() returns a str from Path object.

And I think that it is often better to use relative paths - they will work for everyone without the need to update them. You can for example create a data folder in your workspace with the script and put the file there.

Also, you can check this out for more details regarding paths in Python.

tpwo
  • 484
  • 3
  • 12
  • So @trivvz correct me if I am misunderstanding but best practice would be to try: os.path.join('C:\Users\nulli\OneDrive\Desktop\, Work_Sample.csv') – iceAtNight Mar 05 '21 at 21:31
  • I'll suggest always using standard slashes and the library. So it should be (you have a missing `): os.path.join("C:/Users/nulli/OneDrive/Desktop", "Work_Sample.csv") – tpwo Mar 05 '21 at 21:38
  • Probably, it is also better nowadays to use pathlib rather than os.path. It is newer and it is easier to do more complicated stuff with it. – tpwo Mar 05 '21 at 21:39
  • so something similar to what anurag has said? My only problem with that is I get a no such file in directory when I try to read in my pd.read_csv line – iceAtNight Mar 05 '21 at 21:47
  • @iceAtNight, the following is the simplest that will work: csv = r'C:\Users\nulli\OneDrive\Desktop\Work_Sample.csv'. But notice that you imported the module with the same name. If you would like to use it later, it won't be possible as your string overwrites it. Always choose different names for your variables - it's a good practice – tpwo Mar 05 '21 at 21:47
  • Gotcha! This is odd because I am still having a no such file in directory error at df = pd.read_csv(csv, sep=',') – iceAtNight Mar 05 '21 at 21:51
  • @iceAtNight maybe try using a relative path instead? It's usually an easier and better idea. So put the file in the same folder where you have your Python script and use the filename as the path (no slashes needed :D) – tpwo Mar 05 '21 at 22:00
  • shoot! I am using google Colab so I am basically working from an online notebook – iceAtNight Mar 05 '21 at 22:02
  • Oh, so we got the answer. Colab doesn't see any of your folders and files :) But it should be possible to upload files to Colab. Check this out: https://towardsdatascience.com/3-ways-to-load-csv-files-into-colab-7c14fcbdcb92 – tpwo Mar 05 '21 at 22:06
0

use the following:

from pathlib import Path

csv = str(Path('C:\\Users\\nulli\\OneDrive\\Desktop\\Work_Sample.csv'))
anurag
  • 1,715
  • 1
  • 8
  • 28
  • to be clear I am using csv_file = str(Path('C:\\Users\\nulli\\OneDrive\\Desktop\\Work_Sample.csv')) df = pd.read_csv(csv_file, sep=',') is what I want to be using with obviously from pathlib import Path at the top? I still get a no such file in directory error for this when I use pd.read_csv? df = pd.read_csv(csv_file, sep=',') – iceAtNight Mar 05 '21 at 21:45
  • are you on windows or linux? Is the path correct? – anurag Mar 05 '21 at 21:58
  • I am on windows using google colab, the path should be correct. I have updated the question if that helps. So you should be able to see what I am trying as of now – iceAtNight Mar 05 '21 at 22:00
  • is the file on your desktop or on colab? And are you running your code on local machine or on colab? colab is running on google's servers, you have to first upload the file. – anurag Mar 05 '21 at 22:06
  • I actually just got it! I needed to upload the file in colab! – iceAtNight Mar 05 '21 at 22:12
0

After adding the slash after 'C:', I would recommend trying /'s instead of \'s in your path.