Linux: Using python, how can I get a list of software that can open a certain file type?

Viewed 138

I'm creating a GUI program with python and PyQt5, I want to create an "Open with:" option for it. How do I get a list of all installed software that can run a specific file type? In this case the file type would be image.

enter image description here

2 Answers

The MIME type of the file

Are you able to figure out or specify the mime-type of the file(s) in question?

Besides a more approximate hint like filename extension (mostly used on Windows) the MIME type is the association to find a program capable opening the file (and its content).

You could use some Python package to figure out the MIME type of a file, e.g.:

Find programs associated with certain MIME types (Linux)

If you are looking for a solution on Linux only, I would recommend:

Or try to collect the MIME related info stored in Desktop entry files (.desktop) commonly in folder /usr/share/applications/, as commented by Mortz. As these entries are specified by Freedesktop.org also abbreviated as "XDG" they should be pretty independent from the Linux flavor or desktop-environment.

Going through some other SO posts based on the answers on this post, I happened across the mailcap library in python. It was basically what I needed, but not quite. Then finally, though my GUI is made with PyQt5, I could get what I wanted with Gio from PyGtk. Code to get it:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gio

print(Gio.app_info_get_all_for_type('image/png'))
Related