determining file type

R

Ritesh Raj Sarraf

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.

Thanks,
Ritesh
 
M

Maric Michaud

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
 
R

Ritesh Raj Sarraf

But isn't there any library function ?
Something like
XX.filetype(name)
Directory
File-Tar
File-Zip
File-MPEG

Ritesh


Maric said:
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
 
R

Ritesh Raj Sarraf

Also,
f = file ('some_file.jpg')

throws an error.
"str object is not callable"

Ritesh

Maric said:
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
 
M

Maric Michaud

Le Mercredi 14 Juin 2006 12:41, Ritesh Raj Sarraf a écrit :
Also,
f = file ('some_file.jpg')

throws an error.
"str object is not callable"
stange, did you define a function named file ?
You can use open instead.

--
_____________

Maric Michaud
_____________

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

Marc 'BlackJack' Rintsch

Ritesh Raj said:
Also,
f = file ('some_file.jpg')

throws an error.
"str object is not callable"

This happens if you rebind names of builtins. In this case to a string.
Don't do that!

In [5]:file
Out[5]:<type 'file'>

In [6]:file = 'foo.txt'

In [7]:file
Out[7]:'foo.txt'

In [8]:file('x')
---------------------------------------------------------------------------
exceptions.TypeError Traceback (most recent call last)

/home/bj/<ipython console>

TypeError: 'str' object is not callable


Ciao,
Marc 'BlackJack' Rintsch
 
S

Sion Arrowsmith

Ritesh Raj Sarraf said:
Also,
f =3D file ('some_file.jpg')

throws an error.
"str object is not callable"

You know all the times people say in this group "Don't use list
or str or file[HINT] or anything else that shadows a built in as
a variable name"? Now you know why.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top