wishlist: more general file objects, and some doc improvements

Dan Schmidt dfan at harmonixmusic.com
Wed May 5 11:20:11 EDT 1999


"Haimo G. Zobernig" <Haimo.Zobernig at cern.ch> writes:

| Currently you create a file object by using the open() function e.g.
| 
| f = open("myfile.txt","r")
| 
| later you can apply that object's methods e.g. f.write(), f.close() 
| But this is not very symmetric nor object oriented. 
| I would like to be able to create file objects independent of whether 
| the file is being opened, or whether it exists at all. Something like
| 
| f = file("myfile.txt") 
| 
| file object f should then also have an open() method
| and a number of attributes such as exists(), is_open(), etc.
| so that later I can do things like
| 
| for f in list_of_files:
|    if f.exists():
|       f.open("r")
|       x = f.readlines()
|       f.close()
|    else:
|       f.open("w")
|       f.write(some_data)
|       f.close()
|    
| It might also be useful to provide a "inquire" method which could
| e.g. return a dict of a file's currently known properties, with a
| subset of properties being defined on all supported platforms,
| and the others being platform dependent.

'file' currently means 'successfully opened file'.  I would prefer not
to change this meaning to 'name of a file which may or may not be
open, and might not even exist', although that concept may be useful
too.

Perhaps we could both be satisfied by a 'filename' class, which
represents your concept, and has methods such as exists() and the
tests that Perl refers to with -d, -r, -T, etc.  filename.open("r")
could then return an actual file.

Plus, this could all be done without changing the language.

Your example would then be:

  for fn in list_of_filenames:
     if fn.exists():
        f = fn.open("r")
        x = f.readlines()
        f.close()
     else:
        f = fn.open("w")
        f.write(some_data)
        f.close()

-- 
                 Dan Schmidt -> dfan at harmonixmusic.com, dfan at alum.mit.edu
Honest Bob & the                http://www2.thecia.net/users/dfan/
Factory-to-Dealer Incentives -> http://www2.thecia.net/users/dfan/hbob/
          Gamelan Galak Tika -> http://web.mit.edu/galak-tika/www/




More information about the Python-list mailing list