0

I'm tring to open a file using the following code:

f=open('C:\Users\gabor\Desktop\NPI\test.csv', 'r')
reader=csv.reader(f)
for row in reader:
    print row

Its returning an error:

IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\Users\\gabor\\Desktop\\NPI\test.csv'

I've change 'rb' to 'r' and left it out and I keep getting the same error.

Any suggestions on how to open the file?

  • That's not the problem, it's just how Python displays literal backslashes. The problem is the last one *isn't* doubled, it is a tab... – jonrsharpe Jul 11 '16 at 17:03
  • Python is producing a **representation** of your string object, one that can be used to re-create the same value, safely. Backslashes have special meaning in Python string literals, and doubling them escapes them preventing them from being used as escape sequences. – Martijn Pieters Jul 11 '16 at 17:03
  • 2
    In other words, you need to worry about the place where there is **not** a double backslash, because that's an escape sequence. `\t` is a tab. – Martijn Pieters Jul 11 '16 at 17:04
  • [Why do backslashes appear twice?](http://stackoverflow.com/q/24085680/2301450) – vaultah Jul 11 '16 at 17:06

1 Answers1

1

I think you need double back-slash in your path. Or you can put "r" before the string as: f=open(r'C:\Users\gabor\Desktop\NPI\test.csv', 'r').

pathoren
  • 1,634
  • 2
  • 14
  • 22
  • that cleared the initial error but raised a new one: Error: new-line character seen in unquoted field - do you need to open the file in universal-newline m – SarahRose Jul 11 '16 at 17:17