2

I am trying to import files from the same folder, but put the r'location' on separate lines.

import pandas as pd
extra=r'C:\Users\Desktop\Pandas\'
visits=pd.read_csv(extra+r'visits.csv')

I get the EOL while scanning string literal error.

Trenton McKinney
  • 43,885
  • 25
  • 111
  • 113
Nick
  • 131
  • 2
  • 11

3 Answers3

0

This run:

import pandas as pd
extra=r'C:\Users\Desktop\Pandas\\'
visits=pd.read_csv(extra+r'visits.csv')

the backslash \ as the escape character before '. In general, in python it is strongly recommended to use / instead of escape character \

Massifox
  • 3,859
  • 9
  • 29
0

Despite using raw string syntax, the syntax \' still escapes the last quote and tricks Python into thinking your string never ends.

You can just concatenate that last slash:

import pandas as pd
extra=r'C:\Users\Desktop\Pandas' + "\\"
visits=pd.read_csv(extra+r'visits.csv')
miike3459
  • 1,380
  • 2
  • 15
  • 30
0

\ is an escape character which makes it possible to use ' within a string instead of marking its end.

I'd generally recommend / as path separator in python - it prevents you from exactly this type of annoying side effects.

extra = 'C:/Users/Desktop/Pandas/'
visits = pd.read_csv(extra + 'visits.csv')
SpghttCd
  • 10,015
  • 2
  • 16
  • 23
  • 1
    better still: don't end a path in a (back) slash and use `os.path.join()`. Even better still: use `pathlib`: `from pathlib import Path` and `extra = Path(r"C:\Users\Desktop\Pandas")` and `extra / 'visits.csv'`. – Martijn Pieters Sep 17 '19 at 21:46