[Tutor] code improvement for beginner ?

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Sun Oct 9 11:01:21 CEST 2005


Danny Yoo wrote:
> Looking at pageimgs(): I'm not sure what 't' means in the open statement:
> 
> 	f = open(filename, "rt")
> 
> and I think that 't' might be a typo: I'm surprised that Python doesn't
> complain.  Can anyone confirm this?  I think you may have tried to do "r+"
> mode, but even then, you probably don't: you're just reading from the
> file, and don't need to write back to it.

It's not a typo. 't' opens the file in text mode. It doesn't make any
difference on Unix, but it does on Windows. As you might know, lines in
text file end with '\r\n' in Windows rather than only '\n'. If you open
a file in text mode, the line endings are translated to '\n' on input
and back to '\r\n' on output.

One can also specify 'b' instead to open the file in binary mode
instead, which will not do any translation. I'm not sure which one of
the two is the default.

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven



More information about the Tutor mailing list