[Python-Dev] cpython: Issue #12451: Add support.create_empty_file()

Victor Stinner victor.stinner at haypocalc.com
Fri Jul 1 13:12:07 CEST 2011


Le vendredi 01 juillet 2011 à 11:24 +0200, Antoine Pitrou a écrit :
> On Thu, 30 Jun 2011 23:25:59 +0200
> victor.stinner <python-checkins at python.org> wrote:
> > http://hg.python.org/cpython/rev/0c49260e85a0
> > changeset:   71103:0c49260e85a0
> > user:        Victor Stinner <victor.stinner at haypocalc.com>
> > date:        Thu Jun 30 23:25:47 2011 +0200
> > summary:
> >   Issue #12451: Add support.create_empty_file()
> > 
> > We don't need to create a temporary buffered binary or text file object just to
> > create an empty file.
> 
> Is there a reason for this?

The code was correct. I think that a function with an explicit name is
better than the open().close() pattern (which has various flavours, see
the diff). This pattern came sometimes with a comment explaining what it
does (create an empty file), which let me think that it's not easy to
understand what it is supposed to do (if you don't have the comment).

My initial need was to make quiet a warning (of my patched Python, see
#12451) if a file is opened in text mode without an explicit encoding.

> I find it quite explicit and obvious what the following does:
> 
>     with open("somefile", "wb"):
>         pass

For "wb", the only gain is to avoid the creation of temporary FileIO and
BufferedWriter objects, a micro optimisation. Most of the time, "w" mode
was used, so another temporary TextIOWrapper object was also created. I
also saw "w+" mode (use BufferedRandom+TextIOWrapper objects).

Victor



More information about the Python-Dev mailing list