1

I just started programming in python yesterday.

Ultimately I want to open a comma deliminated file and read its contents. I searched for related topics and tried using some of the code and am running into some errors.

Code:

def ReadTxtFile():
    fname="c:\vba\lapseC2.csv"
    #with open(fname) as f:
    with open("c:\vba\lapseC2.csv", "r") as f:
        content = f.readlines

you may also want to remove whitespace characters like \n at the end of each line

content = [x.strip() for x in content] 

ReadTxtFile()

Error:

OSError: [Errno 22] Invalid argument: 'c:\x0bba\lapseC2.csv'

Andrew_Lvov
  • 4,621
  • 2
  • 25
  • 31
massmutual
  • 25
  • 1
  • 4
  • hello massmutual. please paste in your code in the textbox, then select it and press ctrl+K - i wonder if your python indents are wrong or if you have not pasted it properly in the stackoverflow textbox. – Rinogg Sep 12 '18 at 14:40
  • it fails because `\v` is interpreted as a vertical tabulation. – Jean-François Fabre Sep 12 '18 at 14:53

1 Answers1

2

The backslash works as an escape symbol so if you want to insert it as a part of string, you should escape backslash itself:

fname="c:\\vba\\lapseC2.csv"

Another option is to use slashes, which seem to be supported by most popular operating systems:

fname="c:/vba/lapseC2.csv"
Andrew_Lvov
  • 4,621
  • 2
  • 25
  • 31