-1

I am simply trying to define a path and file name then use pandas.read_csv() in the variable display of spyder, the path and file name appear correct, but in reality they have double \\. I know this has got to be something really stupid...

siteinfopath=r'C:\Users\cpsei\Documents'
siteinfofile=siteinfopath+'\grav_stats.csv'
grav_stats=pd.read_csv(siteinfofile)

When i run the script I get the following error message:

FileNotFoundError: [Errno 2] File b'C:\Users\cpsei\Documents\grav_stats.csv' does not exist: b'C:\Users\cpsei\Documents\grav_stats.csv'

and when I type

siteinfofile
Out[145]: 'C:\\Users\\cpsei\\Documents\\grav_stats.csv'

Why the double \. In the variable viewer the path is correct.

rsm
  • 2,530
  • 4
  • 26
  • 33
Chloe
  • 19
  • 2
  • Possible duplicate of [Windows path in Python](https://stackoverflow.com/questions/2953834/windows-path-in-python) – Håken Lid Aug 12 '19 at 19:25

1 Answers1

2

You see double \\ instead of one, because \ is used in python as escape character - it informs that this \ character and next character should be threated in special way. For example:

  • \t - means TAB
  • \r - is carriage return - cursor moves to the beginning of the line
  • \n - is new line - cursor moves to beginning of new line

If however you want just plain simple \, you have to use \\ - first one informs as usual that there is some special character, and next informs that this special character is actually \.

You can read more about it ie on https://docs.python.org/3/tutorial/introduction.html#strings - there is a lot of very good examples :)

So, everything is OK, your strings work as expected. If you want to see how this string looks like, and not how it is constructed, print it:

>>> print(siteinfofile)
C:\Users\cpsei\Documents\grav_stats.csv

Are you sure path is correct and you can read this file? That's the only advice I can think of here...

rsm
  • 2,530
  • 4
  • 26
  • 33