PEP for fp.type()

Tim Cera timcera at
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
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

To replace imghdr and sndhdr instead of...

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

would be something like...

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

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

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.


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


>>> 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

More information about the Python-list mailing list