#title Finding the MIME type of a file ||<>|| [[http://en.wikipedia.org/wiki/MIME|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. {{{#!python #!/usr/bin/env python # Import the GNOME VFS module import gnomevfs # Give the URI of the file you want to find the MIME information for. file_uri = "file:///home/user/hardy-desktop-i386.iso" # Get the MIME type of the specified file file_mimetype = gnomevfs.get_mime_type(file_uri) # Output the file information: # File MIME type print "File type:", file_mimetype # The default application used to open this type of file print "Default Application:", gnomevfs.mime_get_default_application(file_mimetype)[1] # Print all applications which are registered to handle this file type print "Other Applications:" for item in gnomevfs.mime_get_all_applications(file_mimetype): # Output app name [1] and command [2] print "\t", item[1], "(Command:", item[2], ")" # Print the human-readable description of this file type 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 == * The file URI can be any valid URI, such as ''file:///home/username/file.txt'' or ''http://www.ubuntu.com'' * GNOME VFS will soon be replaced by GVFS (see [[http://library.gnome.org/devel/gio/2.15/ch15.html|Migrating from GnomeVFS to GIO]]) == Further Reading == * [[http://www.pygtk.org/pygnomevfs/|Python gnomevfs module reference]] * [[http://library.gnome.org/devel/gio/2.15/ch15.html|Migrating from GnomeVFS to GIO]] * [[http://en.wikipedia.org/wiki/MIME|Wikipedia article on MIME]] * [[http://en.wikipedia.org/wiki/Uniform_Resource_Identifier|Wikipedia article on URIs]] ---- Categories: CategoryPythonRecipes Parent: [[ProgrammingPython]] | [[/PageDiscussion|Discuss this page]]