determining file type

Maric Michaud maric at aristote.info
Wed Jun 14 05:53:29 EDT 2006


Le Mercredi 14 Juin 2006 11:22, Ritesh Raj Sarraf a écrit :
> Hi,
>
> I have a funtion named unzipper() which does the work of unzipping the
> files.
>
> Is there a way I can identify what is the type of the file which'll be
> passed to unzipper().
> If yes, I'll be able to make out if it's a zip file or a tar or a bz2
> file.
>
Under Linux you can use the file command in a pipe.
In all cases, you use the "magic" repository of the file command directly like 
this (I found the '\xff\xd8' for jpeg in /usr/share/file/magic.mime on my 
debian)

In [69]: f = file ('samurai_tux.jpg')

In [70]: s = f.read(2)

In [71]: s == '\xff\xd8'
Out[71]: True




> Thanks,
> Ritesh

-- 
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097



More information about the Python-list mailing list