puzzle about file Object.readlines()

Dave Angel davea at ieee.org
Sat Mar 19 15:05:49 EDT 2011


On 01/-10/-28163 02:59 PM, MRAB wrote:
> On 19/03/2011 13:15, 林桦 wrote:
>> i use python 2.5. os is window 7.
>> the puzzle is :python don't read the leave text when meet character:
>> chr(26)
>> the code is:
>> /fileObject=open('d:\\temp\\1.txt','w')
>> fileObject.write('22222222222222\r\n')
>> fileObject.write(chr(26)+'\r\n')
>> fileObject.write('33333333333333')
>> fileObject.close()
>> fileObject=open('d:\\temp\\1.txt','r')
>> i=0
>> for line in fileObject:
>> i+=1
>> print str(i)+'******'+line
>> fileObject.close()/
>>
>> the output only print:
>> />>>
>> 1******22222222222222/
>>
>> but can't print next line text:/'33333333333333'' 。who tell me why?
>> /
>>
> chr(26) can sometimes be used in text files to indicate the end of the text.
>
> In Microsoft Windows it dates from MS-DOS, which borrowed from CP/M, an
> operating
> system which stored the file size as the number of 128-byte records.
> chr(26) was used to
> indicate where the text ended in the last record.
>

On Win a ctrl-z is end of file.  So if you want to read beyond the end 
of a text file, you have to pretend it's binary.  Open it with  "rb" 
instead of "r"

DaveA




More information about the Python-list mailing list