How do get the GTK mime-type/content-type information from the file extension
I'm not allowed to access the file content (for performance reason开发者_如何学编程s), so using GFile is not an option.
you need g_content_type_guess.
In Python (GTK2):
import gio
gio.content_type_guess('foo.pdf')
This will return application/pdf
In Python (GTK3):
from gi.repository import Gio
content_type, val = Gio.content_type_guess('filename=foo.pdf', data=None)
content_type will contain application/pdf
More details in the documentation: http://developer.gnome.org/gio/stable/gio-GContentType.html#g-content-type-guess
Looks like GIO's content type detection is based on the file extension (when there is an extension).
$ ./file /bin/sh
application/x-executable
$ cp /bin/sh a.wav
$ ./file a.wav
audio/x-wav
where ./file is
#!/usr/bin/env python2
import sys, gio
f = gio.File(sys.argv[1])
info = f.query_info('standard::content-type')
print info.get_content_type()
精彩评论