[Q] File Object -- function 'read'

Bjorn Pettersen BPettersen at NAREX.com
Thu Jul 19 12:41:28 EDT 2001


> From: jackyci [mailto:jackyci at sinamail.com]
> 
> Hi 
> 
> There is a TEXT file named 'test.txt'.
> --------test.txt-------
> This is a test.\n
> test\n
> test\n
> -----------------------
> In Windows. "\n" equal "\x0d\x0a"
> But in function "read" of File Object, "\x0d\x0a" equal 1 byte.
> 
> file = open('test.txt')
> str = file.read(22)    # I want to read the first two line
> 
> # but str = This is a test\ntest\nte
> 
> How to solve the problem ?

Use:
 
  file = open('test.txt', 'rb')

to open the file in binary mode (i.e. no translation of \n).

-- bjorn




More information about the Python-list mailing list