Search   Feed   Browse   Add
Feed items 1 - 9 of 9 for May 2008

Comments on: Opening a file in Python

Wherein I write some stuff that you may like to read. Or not, its up to you really.

By: Steve Kryskalla - May 9, 2008

I think you could also turn this type of function into a decorator that will automatically coerce an argument of the wrapped function.
http://halfcooked.com/blog/2008/05/09/opening-a-file-in-python/#comment-32785

By: Steve Kryskalla - May 9, 2008

Another version: def getfile(obj): &8230;.if all(hasattr(obj, attr) for attr in 'read', 'seek', 'write'): &8230;&8230;..return obj &8230;.elif isinstance(obj, basestring) and os.path.isfile(obj): &8230;&8230;..return file(obj) &8230;.else: &8230;&8230;..raise ValueError(&8221;Not a file-like object or valid path.&8221;)
http://halfcooked.com/blog/2008/05/09/opening-a-file-in-python/#comment-32784

By: Snazz - May 9, 2008

You could use named arguments def my_function(file_name=None, file_object=None): &8230;.if file_object == None: &8230;&8230;..file_object = open(file_name&8217;, &8216;r&8217;) &8230;. Do stuff with file_object my_function(file_name=&8217;test.txt&8217;) my_function(file_object=open(&8217;test.txt&8217;,'r&8217;)
http://halfcooked.com/blog/2008/05/09/opening-a-file-in-python/#comment-32779

By: Dave Kirby - May 9, 2008

If the code is only going to read the data in the file a line at a time then I would say it is far better to specify that the function takes an iterable sequence of strings, so that the client code is responsible for creating the file object and passing it in. This way the client could equally give the function a urlib object, or a StringIO object, or a list of strings, or a generator, or anything else that the user can come up with. This not only makes the function more flexible and useful,...
http://halfcooked.com/blog/2008/05/09/opening-a-file-in-python/#comment-32777

By: Paddy3118 - May 9, 2008

Isn&8217;t the tryexcept to be preferred over isinstancehasattr Or will it lead to bad logic The OP&8217;s case will assume its a file object if it is not open-able; as opposed to assuming it is a file object if it is an instance of basestring. I suspect that in this case it amounts to the same thing if open itself only works if given an instance of basestring, but style-wise I thought it was preferable to limit the use of isinstancehasattr Hell, I&8217;m nit-picking. either would work! - Paddy.
http://halfcooked.com/blog/2008/05/09/opening-a-file-in-python/#comment-32775

By: Michael Foord - May 9, 2008

I agree that in this circumstance a type check ( isinstance(file_name_or_object, basestring) ) is perfectly acceptable. ConfigObj does this. (Particularly as there is no &8217;string protocol&8217; and so any filename will have to be passed in as a real string or at least a subclass.) Michael
http://halfcooked.com/blog/2008/05/09/opening-a-file-in-python/#comment-32773

By: vArDo - May 9, 2008

me: why do you use &8216;hasattr&8217; with &8216;elif&8217; Why not simply use &8216;isinstance(file_name_or_object,file)&8217; Is it faster I&8217;m asking because many classes can have &8216;read&8217; method, not necessarily being &8216;file-like&8217;.
http://halfcooked.com/blog/2008/05/09/opening-a-file-in-python/#comment-32772

By: Steven - May 9, 2008

What about this It would depend of course in how &8220;filelike&8221; the object has to be. I don&8217;t know whether it is considered the most idiomatic, but it&8217;s used for example in xml.etree.Elementree for the parse function. if not hasattr(file_name_or_object, &8216;read&8217;): filename_or_object = open(file_name_or_object) return filename_or_object
http://halfcooked.com/blog/2008/05/09/opening-a-file-in-python/#comment-32771

By: me - May 9, 2008

def my_function(file_name_or_object): parameter is string if isinstance(file_name_or_object, (str, unicode)): return open(file_name_or_object) parameter is an file like object that has at least a read method elif hasattr(file_name_or_object, &8220;read&8221;): return file_name_or_object
http://halfcooked.com/blog/2008/05/09/opening-a-file-in-python/#comment-32770
Available Archives
- May (9 items)
Sponsored Links
© 2008 FeedCapsule.com  |  Contact