0

I get a file not found error when I use a variable with raw string literal appended below, I believe because the file path contains \f which needs to be escaped, I want to have the path read as raw literal so have attempted to add the 'r' prefix to the path string manually and assign this to the variable I've called path as below, but it doesn't work.

import pandas as pd
path = raw_input("Enter location of data file:")
path = str('r"'+  str(path[1:]))
print path

try:
    df=pd.read_excel(path)
except:
    df= pd.read_csv(path)
df.head 

prints r"C:\Users\faulknerdw\Anaconda\Progs\GW_data.csv"

but it works if I input what appears to be the exact same thing long-hand. e.g.

df=pd.read_excel(r"C:\Users\faulknerdw\Anaconda\Progs\GW_data.csv")

and

df= pd.read_csv(r"C:\Users\faulknerdw\Anaconda\Progs\GW_data.csv")

What's going on?

flashliquid
  • 500
  • 9
  • 23
  • Note: I am expecting the user input of the file path to have quotes e.g. "C:\Users\..." as a result of the "copy as path" command in Windows. – flashliquid Jul 05 '18 at 06:15
  • `r` is not a part of a string. It is simply an instruction to Python interpreter to treat `\\` in a special manner. – DYZ Jul 05 '18 at 06:26
  • @DyZ Thanks, yes I know, the question is, how do I formulate my filepath variable, created from 'raw_input' so that read_csv recognizes it as a string literal filepath? – flashliquid Jul 05 '18 at 06:34
  • You do not need the line `path = str('r"'+ str(path[1:]))`. Simple take it out. The rest should work. – DYZ Jul 05 '18 at 06:35
  • @DyZ Unfortunately it doesn't work because my file path contains \f which needs to be escaped (hence attempting to use the 'r' prefix to have it read as raw). – flashliquid Jul 05 '18 at 06:40
  • Please add an example of the offending path to the question. Other wise, it is not clear what you are trying to accomplish. – DYZ Jul 05 '18 at 06:41
  • @DyZ the offending path is in the example multiple times. I will highlight the issue though. – flashliquid Jul 05 '18 at 06:43
  • `raw_input()` will correctly read '\f' as _two_ characters '\' and 'f'. There will be no confusion. Give it a try. – DYZ Jul 05 '18 at 06:45
  • @DyZ No, doesn't work: IOError: File "C:\Users\faulknerdw\Anaconda\Progs\GW_data.csv" does not exist, but if I enter the string manually it finds and reads the file. – flashliquid Jul 05 '18 at 06:49
  • That's bizarre. If '\f' were indeed interpreted incorrectly, it would be displayed as '\x0c', not as '\f'. – DYZ Jul 05 '18 at 06:53
  • @DyZ I get the same error if I omit the 'r' prefix when inputting the file path manually. – flashliquid Jul 05 '18 at 07:32

1 Answers1

0

Thanks to @DrawT for this answer: https://stackoverflow.com/a/35920186/6108107

path = path.replace('\\','/') # replaces backslashes with forward slashes
path = path[1:len(path)-1] # to remove quote marks

replaces the backslashes with forward slashes which are still recognised as a filedpath but don't need escaping, then cleans the quotes.

pandas will then accept that file path variable as the location of a excel file or csv.

flashliquid
  • 500
  • 9
  • 23