MIME is a system for identifying file types. On Ubuntu, information about file types and default applications are stored in the MIME database.

You can use Python and GNOME VFS to find information about files.

Example Code

Example Python code to print various file type information about a file to the Terminal.

   1 #!/usr/bin/env python
   2 # Import the GNOME VFS module
   3 import gnomevfs
   4 
   5 # Give the URI of the file you want to find the MIME information for.
   6 file_uri = "file:///home/user/hardy-desktop-i386.iso"
   7 # Get the MIME type of the specified file
   8 file_mimetype = gnomevfs.get_mime_type(file_uri)
   9 
  10 # Output the file information:
  11 # File MIME type
  12 print "File type:", file_mimetype
  13 
  14 # The default application used to open this type of file
  15 print "Default Application:", gnomevfs.mime_get_default_application(file_mimetype)[1]
  16 
  17 # Print all applications which are registered to handle this file type
  18 print "Other Applications:"
  19 for item in gnomevfs.mime_get_all_applications(file_mimetype):
  20         # Output app name [1] and command [2]
  21         print "\t", item[1], "(Command:", item[2], ")"
  22 
  23 # Print the human-readable description of this file type
  24 print "Description:", gnomevfs.mime_get_description(file_mimetype)

Example Output

The output of the example code above (for an ISO file):

File type: application/x-cd-image
Default Application: CD/DVD Creator
Other Applications:
        CD/DVD Creator (Command: nautilus-cd-burner --source-iso= )
        Brasero Disc Burning (Command: brasero )
        Archive Manager (Command: file-roller )
Description: raw CD image

Notes

Further Reading


Categories: CategoryPythonRecipes Parent: ProgrammingPython | Discuss this page

PythonRecipes/MIMETypes (last edited 2011-04-09 03:57:08 by k.dejong)