Filename type (Was: Re: finding file size)

Gerrit Holl gerrit at nl.linux.org
Sat Jan 3 05:17:34 EST 2004


Hi,

I propose to add a "filename" type to Python.

Martin v. Loewis wrote:
> Sean Ross wrote:
> >My question is this: Is there a reason why file objects could not have a
> >size method or property?
> 
> Yes. In Python, file objects belong to the larger category of "file-like
> objects", and not all file-like objects have the inherent notion of a 
> size. E.g. what would you think sys.stdin.size should return (which
> actually is a proper file object - not just file-like)?

A different solution to this problem would be to introduce "filename"
type to Python, a subclass of str. The "name" attribute of file would be of this
type. This type would inherit a lot of os.path stuff: getsize becomes
simpler, more readable, and more object oriented, as do other os.path
functions. I think the alternatives look a lot more prety:

OLD                                         NEW
os.path.realpath(fn)                        fn.realpath()
os.path.getmtime(fp.name)                   fp.name.getmtime()
os.path.ismount(os.path.dirname(fp.name))   fp.name.dirname().ismount()

It's more beatiful, simpler, flatter (#3), practical, obvious, easy.

problem: what do do with os.path constants?
solution: make them class attributes
problem: how to handle posixpath, ntpath, macpath?
solution: abstract Path class with NTPath, MacPath, PosixPath sublasses which is the actual type of e.g. fn.name on a certain platform
problem: backwards compatibility
solution: same as string methods
problem: "/dev/null" reads as a Path but is a str
solution: path("/dev/null") is a little more typing for a lot more luxery
problem: what to do with commonprefix?
solution: don't know
problem: what to do with os.path.walk?
solution: use os.walk instead
problem: what to do with sameopenfile?
solution: make it a file method
problem: what to do with join, split?
solution: rename to joinpath, splitpath.

Any comments?

yours,
Gerrit.

-- 
158. If any one be surprised after his father with his chief wife, who
has borne children, he shall be driven out of his father's house.
          -- 1780 BC, Hammurabi, Code of Law
-- 
Asperger's Syndrome - a personal approach:
	http://people.nl.linux.org/~gerrit/english/




More information about the Python-list mailing list