Puzzled: Y am I ending up with extra bytes?

David C. Ullrich ullrich at math.okstate.edu
Mon Feb 25 15:34:17 EST 2002

On Sun, 24 Feb 2002 23:26:33 -0800, Tim Roberts <timr at probo.com>

>"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.

If we were only giving filenames to the system and never getting
filenames from the system right. But if, say, you have a list a 
filenames you got from Windows they're going to have backslashes
in them, and if, say, you try to filter out the ones containing
'/somedirectory/' instead of the ones containing '\somedirectory\'
it's not going to work; seems like at some point we're going to
have to deal with backslashes in strings regardless (so I always
use backslashes, else I have no chance getting things right.)

>- Tim Roberts, timr at probo.com
>  Providenza & Boekelheide, Inc.

David C. Ullrich

More information about the Python-list mailing list