[Tutor] quick question to open(filename, 'r') vs. file(filename, 'r')

bob gailer bgailer at gmail.com
Mon May 4 23:50:55 CEST 2009


PDavid wrote:
> Dear list,
>
> in different books I come across different syntax for dealing with
> files. It seems that open(filename, 'r') and file(filename, 'r') are
> used interchangeably, and I wonder what this is all about. Is there a
> reason why Python allows such ambiguity here?
>   

regarding file, the docs say:

Constructor function for the file type, described further in section 
3.9, ``File Objects''. The constructor's arguments are the same as those 
of the open() built-in function described below.
When opening a file, it's preferable to use open() instead of invoking 
this constructor directly. file is more suited to type testing (for 
example, writing "isinstance(f, file)").

Unfortunately no explanation as to WHY open is preferred. I have long 
wondered that myself.

Perhaps someone with more enlightenment can tell us!

-- 
Bob Gailer
Chapel Hill NC
919-636-4239


More information about the Tutor mailing list