Puzzled: Y am I ending up with extra bytes?

Tim Roberts timr at probo.com
Mon Feb 25 02:26:33 EST 2002


"A.Newby" <deathtospam43423 at altavista.com> wrote:

>Why is this happening? I read large chunks of data from a text file, 
>according to byte locations specified on another file, and for some reason, 
>this function (below), spits out a few extra bytes.
>
>Here's the code, as entered into the Python shell...... 
>
>    	index = map(string.rstrip, open('D:\cgi-bin\indx.txt').readlines())

Danger, Will Robinson!  Although this works by accident, it would NOT have
worked if your file had been "ndx.txt" instead of "indx.txt", beause the
"\n" would have been interpreted as a linefeed (0x0A).

When you need to refer to a file name in windows, you must either:
1. Use forward slashes:   open('D:/cgi-bin/indx.txt')
2. Use double backslashes:  open('D:\\cgi-bin\\indx.txt')
3. Use a "backslash suppressed" string:  open(r'D:\cgi-bin\indx.txt')
   (note the r' prefix )

Personally, I recommend option 1.  All Windows APIs accept forward slashes
in filenames; only the command shells require backslashes.
--
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list