不允许我访问文件内容(出于性能原因),因此不能使用GFile。
发布于 2011-04-29 14:10:57
你需要g_content_type_guess。
在Python (GTK2)中:
import gio
gio.content_type_guess('foo.pdf')这将返回application/pdf
在Python (GTK3)中:
from gi.repository import Gio
content_type, val = Gio.content_type_guess('filename=foo.pdf', data=None)content_type将包含application/pdf
文档中有更多详细信息:http://developer.gnome.org/gio/stable/gio-GContentType.html#g-content-type-guess
发布于 2011-04-20 21:34:41
看起来GIO的内容类型检测是基于文件扩展名的(如果有扩展名)。
$ ./file /bin/sh
application/x-executable
$ cp /bin/sh a.wav
$ ./file a.wav
audio/x-wav其中./file是
#!/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()https://stackoverflow.com/questions/5724080
复制相似问题