473,769 Members | 6,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

determining file type

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

Jun 14 '06 #1
8 2123
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.j pg')

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
Jun 14 '06 #2
But isn't there any library function ?
Something like
XX.filetype(nam e)
Directory
File-Tar
File-Zip
File-MPEG

Ritesh
Maric Michaud wrote:
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 directlylike
this (I found the '\xff\xd8' for jpeg in /usr/share/file/magic.mime on my
debian)

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

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


Jun 14 '06 #3
Also,
f = file ('some_file.jpg ')

throws an error.
"str object is not callable"

Ritesh

Maric Michaud wrote:
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 directlylike
this (I found the '\xff\xd8' for jpeg in /usr/share/file/magic.mime on my
debian)

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

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


Jun 14 '06 #4
Ritesh Raj Sarraf schrieb:
Also,
f = file ('some_file.jpg ')

throws an error.
"str object is not callable"


I bet you have rebound "file" to a string, like this:

file = "some name"
And PLEASE don't top-quote:

http://www.redballoon.net/~snorwood/quote-rant.shtml
Diez
Jun 14 '06 #5
In <11************ **********@u72g 2000cwu.googleg roups.com>, Ritesh Raj
Sarraf wrote:
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.Type Error Traceback (most recent call last)

/home/bj/<ipython console>

TypeError: 'str' object is not callable
Ciao,
Marc 'BlackJack' Rintsch
Jun 14 '06 #6
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
Jun 14 '06 #7
Ritesh Raj Sarraf <ri**********@g mail.com> wrote:
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.

--
\S -- si***@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Jun 14 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1692
by: CTDev Team | last post by:
Hi, We are using Exchange Server 5.5, and have applications written in VB6 and C# that read and process emails. We are experiencing intermittent errors similar to C# Application System.Runtime.InteropServices.COMException (0x80004005): The client
2
3646
by: Garrett | last post by:
Need any help in determining which groups have been given security access to a folder. Searched DirectoryServices to no avail... Any Help?
1
1574
by: Vagabond Software | last post by:
I'm writing an application where then end-user will want to view and print (not edit) TIFF and JPEG images. For now, I simply use the System.Diagnostics.Process.Start method to open the desired image with the application currently associated with that file type. However, sometimes the wrong application is associated with a file type. For example, Microsoft Office Picture Manager will only display the first page of a multi-page TIFF...
1
1713
by: Sirin Azazi | last post by:
I opened an image file (bmp, jpeg etc) using FileOpenDialog and getting its resolution, height and width. Now I want to get type of the file (not looking at the extension, but which is kept inthe header pf the file). But i couldn't find a method to directly get it by .NET command. Do I have to use some APIs? or do you know a method to find out the file type / format?? Thanks.
4
2161
by: petermichaux | last post by:
Hi, I'm hoping for a reason I'm wrong or an alternate solution... I'd like to be able to dynamically include some javascript files. This is like scriptaculous.js library but their solution is broken in Firefox 1.5.0.1 on OS X. What happens with the Scriptaculous library is this In the html document the author only has to include one line
9
1634
by: Clay_Culver | last post by:
I need to find out if an object is a class. Using new style classes this is very easy: class Test(object): pass obj = Test or obj = Test() if type(obj) == type:
9
3091
by: tubby | last post by:
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. VI shows this info in the files: mimetypeapplication/vnd.oasis.opendocument.textPK mimetypeapplication/vnd.oasis.opendocument.presentationPK etc. Not really a Python specific question but, how do you guys do this sort of thing? I've figured out how to...
2
2079
by: pob | last post by:
I was all happy because thru some tutelage of others I created this array to find the newest file in a directory. However, I then realized its great that I now know the latest file, but I need to know the name of the file. Unfortunately, I am only passing the date into the array. Do I need to create a m-d array ? If so how do I create a 2-d array where I do not know how many rows I will be looping thru ?
1
2257
by: Nathan Sokalski | last post by:
I am having trouble determining the MIME type of a file. Does anybody know how to do this for a file programmatically? Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
5
21065
by: Queez | last post by:
Afternoon, I've spent all morning trawling the net looking for an answer to this. In the end, I've had to resort to good old fashioned asking. How do you determine a file's MIME type using VB.NET? The problem is: - I have an ASP.NET web application - I can display a list of all files in a directory on the server (it's a "directory share" for a scanner) - I want the user to be able to select one of the files to import into the...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.