esnadr esnadr - 1 year ago 109
Python Question

Get the default icon for specific file type in Ubuntu OS by python

In my ubuntu PC, and by python script, I need to get the default path of icon of specific file type.

as :

def get_icon_path(extenstion):
return icon_path



N.B: I read this question, which act only with currently existing files.

How Can I Get an Icon or thumbnail for a Specific file

Answer Source

GNU/Linux desktop environments do not assign icons to filename suffixes (extensions). Rather, they assign icons to Internet media types (MIME types), and a file’s type may or may not be determined by its filename suffix (content sniffing may also be involved). You can use the mimetypes module from the standard library to guess the media type from the suffix. Then gio.content_type_get_icon, and from then on it’s as in the question you linked.

import mimetypes

import gio
import gtk

def get_icon_path(extension, size=32):
    type_, encoding = mimetypes.guess_type('x.' + extension)
    if type_:
        icon = gio.content_type_get_icon(type_)
        theme = gtk.icon_theme_get_default()
        info = theme.choose_icon(icon.get_names(), size, 0)
        if info:
            return info.get_filename()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download