1

I have two functions in my program one will take the path from user and another will open the file in that path, this function will open the file

def pdfparser(filename):
    fp = file(filename, 'rb')

when I pass the path from another function it returns:

IOError: [Errno 22] invalid mode ('rb') or filename:'C:\\Users\\user\\PycharmProjects\\advisor\\website\\a.PDF

I know I have to add double splash to the path or (r'path) but my question how can I add r' or double slash into variable since the path will be stored in variable and pass into another function. I need something like s=r'path

any help please

angela
  • 105
  • 1
  • 3
  • 12
  • 3
    The r prefix is only for constants. Once the string has the correct value, no need to specify r prefix again. Are you sure the file exists on your system? Try to print `filename` just before opening it, and copy/paste it in a windows explorer to see it windows finds it. – Jean-François Fabre Jul 04 '16 at 20:37
  • 1
    I am not sure I understand the question (is the IOError really complete? What about the stacktrace), but what does not work with `filename = r'C:\Users\user\PycharmProjects\advisor\website\a.PDF` besides os.path.join() being far cooler ...? – Dilettant Jul 04 '16 at 20:38
  • Your error indicates the filename was correctly specified; all the backslashes are correct. The problem you have is *not* one that can be solved with prefixing `r` somewhere. Note that `r'...'` is just a *syntax* to specify a string value, it is not a separate object type. It just tells the *parser* not to interpret `\..` escape sequences. Because this is a *syntax concept* there is no *dynamic counterpart*. – Martijn Pieters Jul 04 '16 at 20:44
  • @Jean-François Fabre it exists I have tried that but when I pass 'C:\Users\user\PycharmProjects\advisor\website\a.PDF it does not work but when I add double slash or r' works but the idea my path is sorted within a variable so how can I add r' or double space – angela Jul 04 '16 at 20:48
  • @Dilettant I can't add r' into path because my path within variable in another function – angela Jul 04 '16 at 20:53
  • I would recommend using os.path.join but if you receive it already concat then maybe [python : how to convert string literal to raw string literal?](http://stackoverflow.com/questions/7262828/python-how-to-convert-string-literal-to-raw-string-literal) is where you find your way "out" of the dilemma ;-) – Dilettant Jul 04 '16 at 20:56
  • If I know how change single slash into double slash it would be very easy for me – angela Jul 04 '16 at 21:15
  • Show the whole error message. At least until the end of the line. – Stefan Pochmann Jul 04 '16 at 21:20
  • @StefanPochmann fp = file(filename, 'rb') IOError: [Errno 22] invalid mode ('rb') or filename:'C:\\Users\\user\\PycharmProjects\\advisor\\website\\a.PDF – angela Jul 04 '16 at 21:25
  • @angela If that's the whole line, then that's likely your problem. The string starts with `'C:\\Users` but never ends (there's no ending `'`). So I guess there's a line break at the end and it continues on the next line somehow? – Stefan Pochmann Jul 04 '16 at 21:28
  • @StefanPochmann that function it works when I pass path manually include double slash , But when I pass the path from user it gives me that error so l know my mistake which is from single slash I have to double slash but I don't know HOW !!! since my path within variable – angela Jul 04 '16 at 21:37
  • @angela Do `print repr(filename)` right before `fp = file(filename, 'rb')` and show what that prints. – Stefan Pochmann Jul 04 '16 at 21:52
  • it seems it doubled the slash 'C:\\Users\\userl\\PycharmProjects\x07dvisor\\website\\media\transcripts\x07yse.PDF' and same error but when I store it in variable . for example s=repr(filename) then fb=file(s,'rb') it returns 'C:\\\\Users\\\\user\\\\PycharmProjects\\x07dvisor\\\\website\\\\media\\transcripts\\x07yse.PDF'" and same error but change it the slash – angela Jul 04 '16 at 22:10
  • That string is pretty broken, no wonder it doesn't work. No idea how you got the bell character `\x07` and the tab character `\t` in there. Don't try `file(repr(filename), ...)`, that makes no sense. – Stefan Pochmann Jul 04 '16 at 22:34
  • To be clear: The point of using `repr` was to see the value of the string. Not to create double backslashes for you to use. – Stefan Pochmann Jul 04 '16 at 22:38
  • http://stackoverflow.com/questions/16010992/how-to-use-directory-separator-in-both-linux-and-windows may help. – boardrider Jul 05 '16 at 10:58

1 Answers1

1

Do this:

Before calling this function, store the path value in any variable say, 'directory', here u add the prefix r' in filepath. When calling this pdfparser(filename) function, just pass 'directory' as argument. It works!

>directory= r"C:\Users\SomeUser\Pictures\Disk"  
>pdfparser(directory)