[New-bugs-announce] [issue16122] Allow *open* to accept file-like objects

samwyse report at bugs.python.org
Wed Oct 3 23:30:08 CEST 2012


New submission from samwyse:

I'm once again frustrated by a third-party module that only accepts filenames, not already-opened file-like objects.  This prevents me from passing in StringIO objects or any of the standard file streams.  Currently, *open()* function accepts strings or (in Python 3.X) integers.  I propose that *open()* accept "file-like" objects, either returning them unchanged, or by returning a wrapper object.  While there are many different types of file-like objects, they all have one characteristic in common: the presence of a .close() method.

A non-wrapped version of open() could be as simple as this:

  try:
    file = original_open(name, mode, buffering)
  except TypeError:
    if hasattr(name, 'close'):
        return name
    raise

Returning a wrapper object would be slightly more complicated, but would allow the wrapped object to remain open even after the wrapper is closed.

----------
components: IO
messages: 171908
nosy: samwyse
priority: normal
severity: normal
status: open
title: Allow *open* to accept file-like objects
type: enhancement
versions: Python 2.7, Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16122>
_______________________________________


More information about the New-bugs-announce mailing list