Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: accepting file path or file object?

Reply
Thread Tools

Re: accepting file path or file object?

 
 
Cameron Simpson
Guest
Posts: n/a
 
      11-05-2012
On 05Nov2012 10:54, andrea crotti <(E-Mail Removed)> 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 <(E-Mail Removed)>

....valve spreeengs? VALVE _*SPREEEEEEEEENGS*_!?! We don' neeeed
no steeeenking valve spreeeeeengs!... - Dr. Desmo
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: accepting file path or file object? Terry Reedy Python 0 11-05-2012 06:30 PM
accepting file path or file object? andrea crotti Python 2 11-05-2012 03:05 PM
Re: accepting file path or file object? Peter Otten Python 0 11-05-2012 01:47 PM
Re: accepting file path or file object? Peter Otten Python 0 11-05-2012 11:49 AM
When did Windows start accepting forward slash as a path separator? Stephen Ferg Python 30 09-30-2003 04:22 AM



Advertisments