“UbuntuHelp:PythonRecipes/MIMETypes”的版本间的差异
来自Ubuntu中文
小 |
小 |
||
(未显示同一用户的1个中间版本) | |||
第2行: | 第2行: | ||
{{Languages|UbuntuHelp:PythonRecipes/MIMETypes}} | {{Languages|UbuntuHelp:PythonRecipes/MIMETypes}} | ||
#title Finding the MIME type of a file | #title Finding the MIME type of a file | ||
− | Parent: [[UbuntuHelp:ProgrammingPython]] | [[UbuntuHelp:/PageDiscussion|Discuss this page]] | + | Parent: [[UbuntuHelp:ProgrammingPython]] | [[UbuntuHelp:PythonRecipes/MIMETypes/PageDiscussion|Discuss this page]] |
[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. | [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. | You can use Python and ''GNOME VFS'' to find information about files. |
2009年5月14日 (四) 17:22的最新版本
点击翻译: |
English |
请不要直接编辑翻译本页,本页将定期与来源同步。 |
- 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
- 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 Migrating from GnomeVFS to GIO)
Further Reading
- Python gnomevfs module reference
- Migrating from GnomeVFS to GIO
- Wikipedia article on MIME
- Wikipedia article on URIs
Categories: