change the first character of the line to uppercase in a text file

Angus Rodgers twirlip at bigfoot.com
Sat Jun 27 13:49:39 CEST 2009


On Sat, 27 Jun 2009 12:13:57 +0100, I wrote:

>Hmm ... the \r\n sequence at the end of a Win/DOS file seems to be
>treated as a single character.

For instance, if test001A.txt is this file:

abc xyz
Bd ef

gH ij

and test001E.py is this:

f = open('test001A.txt', 'r')
for line in f:
   print repr(line)

then the output from "python test001E.py > temp.txt" is this:

'abc xyz\n'
'Bd ef\n'
'\n'
'gH ij\n'

and indeed the output from "print repr(f.read())" is this:

'abc xyz\nBd ef\n\ngH ij\n'

How do you actually get to see the raw bytes of a file in Windows?

OK, this seems to work:

f = open('test001A.txt', 'rb')   # Binary mode
print repr(f.read())

Output:

'abc xyz\r\nBd ef\r\n\r\ngH ij\r\n'

Indeed, when a Windows file is opened for reading in binary mode, 
the length of an "empty" line is returned as 2.  This is starting
to make some sense to me now.

-- 
Angus Rodgers



More information about the Python-list mailing list