Determining when a file is an Open Office Document

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Jan 18 20:22:04 EST 2007


tubby <tubby at bandaheart.com> writes:

> Silly question, but here goes... what's a good way to determine when
> a file is an Open Office document? I could look at the file
> extension, but it seems there would be a better way.

Yes, the name of a file may be useful for communicating with humans
about that file's intended use, but is a lousy, unreliable way to make
a definite statement about the actual contents of the file.

The Unix 'file' command determines the type of a file by its contents,
not its name. This functionality is essentially a database of "magic"
byte patterns mapping to file types, and is provided by a library
called "libmagic", distributed with most GNU/Linux distributions.

    <URL:http://packages.debian.org/testing/source/file>

There is a Python interface to the "magic" functionality. It's in
Debian; I'm not sure if it's part of the "magic" code base, or written
separately to interface with it. Either way, you can get the source
for those packages and find out more.

    <URL:http://packages.debian.org/unstable/python/python-magic>

-- 
 \         "If life deals you lemons, why not go kill someone with the |
  `\  lemons (maybe by shoving them down his throat)."  -- Jack Handey |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list