Why is this code to import a csv not working

I have a csv located here:
G:\My Drive\bak 2024\datasets\icr-identify-age-related-conditions\train-3.csv

Here is the code I tried to import this csv into a dataframe:

import pandas as pd
filepath ="G:\My Drive\bak 2024\datasets\icr-identify-age-related-conditions\train-3.csv"
df = pd.read_csv(filepath)

I get these errors:
FileNotFoundError Traceback (most recent call last) Cell In[9], line 1 ----> 1 df = pd.read_csv(filepath)

I have opened the csv from that location; so I know it’s there.

Any suggestions?

The issue you’re dealing with is that \ is the “escape character”, used for things that are hard to type like \n (a newline) or \t (a tab).

If you print(filepath) you’ll see that it doesn’t match the path you wanted.

One option is to use a raw string r"G:\My Drive..." which doesn’t have escape characters. Another option is to double up on the \ to get the string you actually want: "G:\\My Drive" will print as G:\My Drive

Python disagrees, because it sees a different string. (It would be the same story in most programming languages in common use today; Python did not invent this system, but inherited it from C, with some modifications.) The backslashes (\) have a special interpretation, in combination with the following characters. Usually, you would get an error from this, because most people try to open something that’s in their Users folder, and the \U will be invalid when it isn’t followed by 8 hex digits. But as it happens, the \d and \i in your path string mean what they look like; \b means a backspace character, and \t means a tab.

Starting in Python 3.12, you get a SyntaxWarning for sequences like \d that don’t have a special meaning. Before (since 3.6), you would get a DeprecationWarning, but you normally don’t see these without explicitly opting in to them. In some future Python version (not yet decided AFAIK) they will cause an actual SyntaxError.

General information and advice:

Just thought I’d follow up with my usual clarification: these
alternatives are just expressions for obtaining the filename string
you want.

You want a Python string containing:

 G:\whatever...

The Python expression using a “raw string”:

 r'G:\whatever...'

and the Python expression using a plain old quoted string:

 "G:\\whatever"

with the doubled backslashes are just 2 different expressions for your
intended string value. They just make a str at the end.

Just want to clarify that there’s no such thing as a “raw string” type
of object
; you just get a str. They’re just different ways of writing
that, like 5 and (2+3) are different ways of obtaining the int
with value 5.

We tend to prefer the “raw string” type of expression when we’re dealing
with string values which have backslashes in them, such as Windows paths
and regular expressions.

2 Likes

Simplest is to use forward slash in filenames even on Windows. “C:/users/me/python/whatever/…”. If pasting a copy from elsewhere, then add ‘r’.

Ok great. It works now. Thanks for all your collective help every comment was very helpful.