Reading a file

Gary Herron gherron at
Thu Jul 24 22:27:52 CEST 2008

aditya shukla wrote:
> I have a text file whose contents are like this:-
> jd|fj|dnv|jd|0.33|c:\\windows\\win32
> shcbsbs|nscsjsj|0.93|hsbcjsnc
> I am trying to read the file like this:-
> >>> x = open("c:\\a.txt","r")

Better to use "rb" (for binary read) rather than "r" mode.

> >>>

Now, tell us what you *really* did.  Not that -- that would read the 
contents of the file, put throw the returned result away.
You must have done something like

  c =
> the result that i get is ike this:-
> 'jd|fj|dnv|jd|0.33|c:\\\\windows\\\\win32\nshcbsbs|nscsjsj|0.93|hsbcjsnc\n'
> My doubt is how can i read the file as it is?

You *have* read the file as it is.  Examine each character, and you'll 
see that the string is exactly as you expect.

However, your output to the screen is escaping some character with 
backslashes.  But that's just because of the method you used to print to 
the screen.

What method *did* you use to print?

If you just typed the variable into which you had read the contents, 
then you get the equivalent of

  print repr(c)
which explains the escapes.

  print c
and that won't happen.

Gary Herron

>  ie my output should be
> d|fj|dnv|jd|0.33|c:\\windows\\win32
> shcbsbs|nscsjsj|0.93|hsbcjsnc
> Thanks in advance
> ------------------------------------------------------------------------
> --

More information about the Python-list mailing list