如何获取特定文件的图标或缩略图

Ali*_*imi 1 python linux shell

我正在寻找一种方法来获取与Linux上某种文件类型相关联的图标.

使用shell脚本或python.

我更喜欢在所有平台上运行的本机python方法,但是shell脚本方法可以.

Ali*_*imi 5

我找到了一个解决方案,并且我写了一个函数来完成这项工作

#!/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)
Run Code Online (Sandbox Code Playgroud)

谢谢大家