a newbie here. Could someone tell me why do we use an \'r\' in some cases before the path name in the following function?:
df = pd.read_csv(r\"Path_name\")
<
A raw string will handle back slashes in most cases, such as these two examples:
In [11]:
r'c:\path'
Out[11]:
'c:\\path'
However, if there is a trailing slash then it will break:
In [12]:
r'c:\path\'
File "", line 1
r'c:\path\'
^
SyntaxError: EOL while scanning string literal
Forward slashes doesn't have this problem:
In [13]:
r'c:/path/'
Out[13]:
'c:/path/'
The safe and portable method is to use forward slashes always and if building a string for a full path to use os.path to correctly handle building a path that will work when the code is executed on different operating systems:
In [14]:
import os
path = 'c:/'
folder = 'path/'
os.path.join(path, folder)
Out[14]:
'c:/path/'