[Python-Dev] file() or open()?

John Lenton jlenton at gmail.com
Wed Jul 7 22:47:17 CEST 2004

On Wed, 7 Jul 2004 14:30:33 -0400, François Pinard
<pinard at iro.umontreal.ca> wrote:
> Granted that `open()' may be preferred for backwards compatibility, but
> if it is meant to change or improve in the long run, maybe it should be
> avoided, precisely for future compatibility.
> Currently, I use `file()' when the meaning of my code is to create a
> file object to be operated upon, but to otherwise let Python manage it.
> Yet, I still use `open()' when the meaning of my code is opening a file,
> usually implying that I will also explicitly close it.  If `open()'
> could return objects of various types, and my goal is really to get a
> file object, may I presume that `file()' imposes itself as preferred?

I think the idea is that if you could do

    from urllib import urlopen as open

and your app is still sane, then you're doing it right. For example,
if you used open and then mmap, that might not work in the future
(mmap works only with files, not with things that look slightly like a
file but aren't).

John Lenton (jlenton at gmail.com) -- Random fortune:
bash: fortune: command not found

More information about the Python-Dev mailing list