个人工具

UbuntuHelp:PythonRecipes/MIMETypes

来自Ubuntu中文

跳转至: 导航, 搜索
  1. title Finding the MIME type of a file

Parent: UbuntuHelp:ProgrammingPython | Discuss this page 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

Further Reading


Categories: