PEP for fp.type()

Tim Cera timcera at earthlink.net
Tue Jan 15 12:43:29 EST 2002


I have struggled for some time to get a function similar to the Unix
'file' command.  I found http://www.demonseed.net/~jp/code/magic.py
which works pretty well, but I think that a better place for this
would be as a file method.

I am _timidly_ proposing the following...

>>> fp = open('/home/tcera/.cshrc')
>>> fp.type()
('ascii text', 'text/plain', 'ascii')
>>> fp.close()

I realize that the ascii or binary test is problematic, but if you
take the Unix 'file' approach it would be strictly defined by the
'magic' type.

Also would be valuable to be able to test if the filename extension
and the contents match.

I think this would deprecate the imghdr, sndhdr, and mimetypes
modules.

To replace imghdr and sndhdr instead of...

>>> import imghdr
>>> fp = open('/home/tcera/test.gif')
>>> test = imghdr.what(fp)
>>> test
1
>>>

would be something like...

>>> fp = open('/home/tcera/test.gif')
>>> test = fp.type()[1][:5] == 'image'
>>> test
1
>>>

Not as clear as I would like, but should directly replicate imghdr and
sndhdr.

It would be valuable to me to include the capability to only look at
the filename extension (faster, but could be incorrect).  This
capability would replace the mimetypes module.  Not sure how that
command might look.

Maybe...

>>> fp = open('/home/tcera/test.gif')
>>> fp.type('ext')
('GIF file, v89', 'image/gif', 'binary')
>>> fp.close()

OR

>>> fp = open('/home/tcera/test.gif')
>>> fp.type_ext()
('GIF file, v89', 'image/gif', 'binary')
>>> fp.close()

Dumb idea?  Cool idea?  Comments?

If this idea is thought of kindly, I might be able to spare the time
to write a PEP, but really hope that someone else gloms onto it.

take care
tim



More information about the Python-list mailing list