How to get default application mapped to file extension on Windows using Python

I would like to query Windows using the file extension as a parameter (for example, ".jpg") and return the path to the application windows that were configured as the default application for this file type.

Ideally, the solution would look something like this:

from stackoverflow import get_default_windows_app

default_app = get_default_windows_app(".jpg")

print(default_app)
"c:\path\to\default\application\application.exe"

I am studying the built-in winreg library, which contains registry information for windows, but I had problems understanding its structure, and the documentation is rather complicated.

I am running Windows 10 and Python 3.6.

Does anyone have any ideas to help?

+4
source share
1 answer

. . :

import shlex
import winreg

def get_default_windows_app(suffix):
    class_root = winreg.QueryValue(winreg.HKEY_CLASSES_ROOT, suffix)
    with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r'{}\shell\open\command'.format(class_root)) as key:
        command = winreg.QueryValueEx(key, '')[0]
        return shlex.split(command)[0]

>>> get_default_windows_app('.pptx')
'C:\\Program Files\\Microsoft Office 15\\Root\\Office15\\POWERPNT.EXE'

.

+7

Source: https://habr.com/ru/post/1691590/


All Articles