Ali AlNoaimi Ali AlNoaimi - 1 year ago 105
Python Question

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

I am searching for a way to get the icon associated with a certain file type on Linux.

Either using a shell script or python.

I prefer a native python method which works on all the platforms, but a shell script method would be ok.

Answer Source

I found a solution, and I wrote a function to do the job

#!/usr/bin/env python
import gio, gtk, os

    def get_icon_filename(filename,size):
        #final_filename = "default_icon.png"
        final_filename = ""
        if os.path.isfile(filename):
            # Get the icon name
            file = gio.File(filename)
            file_info = file.query_info('standard::icon')
            file_icon = file_info.get_icon().get_names()[0]
            # Get the icon file path
            icon_theme = gtk.icon_theme_get_default()
            icon_filename = icon_theme.lookup_icon(file_icon, size, 0)
            if icon_filename != None:
                final_filename = icon_filename.get_filename()

        return final_filename

    print get_icon_filename("/home/el7r/Music/test.mp3",64)

thanks all

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download