[Tutor] Undocumented 't' mode flag for open() [Was: Re: code improvement for beginner ?]

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Oct 10 23:38:55 CEST 2005



On Sun, 9 Oct 2005, Roel Schroeven wrote:

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

Hi Roel,

Ah, ok, thank you!  Even so, I don't see this 't' option documented in the
reference docs though, although now I see a reference to this on:

http://mail.python.org/pipermail/python-list/2004-September/241444.html

This is odd that it's not documented in the reference manual.

>From the thread on python-list, it sounded like they left it at depending
on the user to know that 't' is a Windows-specific thing.  But that just
feels off to me: Python's open(), I think, should at least generalize it
so that 't' is explicitely defined in the documentation.


... Wow.  open() actually does seem like a VERY thin wrapper around the
underlying platforms fopen().  For example, on my Linux 2.6 system:

######
>>> open("/etc/passwd", "rfoobar!")
<open file '/etc/passwd', mode 'rfoobar!' at 0x403984e0>
######

So if the underlying fopen() is permissive, we don't see errors.  But I
really think Python should be more strict than the platform.  I'll put it
on my TODO to check to see if this is registered as a bug in SF.




More information about the Tutor mailing list