Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32788 > unrolled thread
| Started by | Cameron Simpson <cs@zip.com.au> |
|---|---|
| First post | 2012-11-06 08:58 +1100 |
| Last post | 2012-11-06 08:58 +1100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: accepting file path or file object? Cameron Simpson <cs@zip.com.au> - 2012-11-06 08:58 +1100
| From | Cameron Simpson <cs@zip.com.au> |
|---|---|
| Date | 2012-11-06 08:58 +1100 |
| Subject | Re: accepting file path or file object? |
| Message-ID | <mailman.3301.1352152738.27098.python-list@python.org> |
On 05Nov2012 10:54, andrea crotti <andrea.crotti.0@gmail.com> wrote:
| Quite often I find convenient to get a filename or a file object as
| argument of a function, and do something as below:
I tend to do this:
def f(fp):
if isinstance(fp, str):
with open(fp) as subfp:
return f(subfp)
... main code using fp as file object ...
That solves the file close issue neatly and lets you put this stuff up
the top where it is obvious.
| So I'm thinking if it's not just worth to skip the support for file
| objects and only use the filenames, which seems a more robust and
| consistent choice..
You can't always use filenames; plenty of calling code will already have
a file-like object to hand (eg a HTTP response or a StringIO or an
already opened file or any of a million other things). So requiring
filenames all the time is unreasonable.
It is almost always etter to write for file objects, since that is what
you would be converting any passed filename into, and put a self call at
the top to convert a filename into a file object if that is a reasonable
use case in your app.
Cheers,
--
Cameron Simpson <cs@zip.com.au>
...valve spreeengs? VALVE _*SPREEEEEEEEENGS*_!?! We don' neeeed
no steeeenking valve spreeeeeengs!... - Dr. Desmo
Back to top | Article view | comp.lang.python
csiph-web