Bonjour,
Je cherche à obtenir le chemin d'accès de l'exécutable qui ouvre un fichier en se basant sur l'extension (Je suis sous Windows). Par exemple:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
app_path = get_app_path('.txt')
>>> r'chemin\vers\notepad.exe'
J'ai actuellement un code (un peu indigeste je reconnais ) qui fonctionne mais pas parfaitement, certaines application ne fonctionnent pas
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import shlex
import winreg
import logging
 
# Configure logging
logging.basicConfig(level=logging.DEBUG, format='%(levelname)s: %(message)s')
 
def get_app_path(ext):
    # Attempt to retrieve from UserChoice
    try:
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\{}\UserChoice'.format(ext)) as key:
            progid = winreg.QueryValueEx(key, 'ProgId')[0]
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'SOFTWARE\Classes\{}\shell\open\command'.format(progid)) as key:
            path = winreg.QueryValueEx(key, '')[0]
            return clean_path(path)
    except FileNotFoundError as e:
        logging.debug(f"UserChoice lookup failed for {ext}: {e}")
 
    # Try HKEY_CLASSES_ROOT
    try:
        class_root = winreg.QueryValue(winreg.HKEY_CLASSES_ROOT, ext)
        if class_root:
            with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r'{}\shell\open\command'.format(class_root)) as key:
                path = winreg.QueryValueEx(key, '')[0]
                return clean_path(path)
    except FileNotFoundError as e:
        logging.debug(f"HKEY_CLASSES_ROOT lookup failed for {ext}: {e}")
 
    # Try HKEY_LOCAL_MACHINE
    try:
        with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Classes\{}\shell\open\command'.format(ext)) as key:
            path = winreg.QueryValueEx(key, '')[0]
            return clean_path(path)
    except FileNotFoundError as e:
        logging.debug(f"HKEY_LOCAL_MACHINE lookup failed for {ext}: {e}")
 
    # Try OpenWithProgids
    try:
        with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r'{}\OpenWithProgids'.format(ext)) as key:
            progid = winreg.EnumValue(key, 0)[0]
        with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r'{}\shell\open\command'.format(progid)) as key:
            path = winreg.QueryValueEx(key, '')[0]
            return clean_path(path)
    except FileNotFoundError as e:
        logging.debug(f"OpenWithProgids lookup failed for {ext}: {e}")
 
    # Try OpenWithList
    try:
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\{}\OpenWithList'.format(ext)) as key:
            progid = winreg.EnumValue(key, 0)[0]
        with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r'{}\shell\open\command'.format(progid)) as key:
            path = winreg.QueryValueEx(key, '')[0]
            return clean_path(path)
    except FileNotFoundError as e:
        logging.debug(f"OpenWithList lookup failed for {ext}: {e}")
 
    # If no path is found, return None
    return None
 
def clean_path(path):
    path = os.path.expandvars(path)
    path = shlex.split(path, posix=False)[0]
    return path.strip('"')
 
# Extensions to check (examples)
extensions = ['.txt', '.pdf', '.jpg', '.png', '.pptx', '.py', '.svg', '.php']
 
# Print the associated program for each extension
for extension in extensions:
    app_path = get_app_path(extension)
    print(f'Extension: {extension}, Program: {app_path}')
RESULAT:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
Extension: .txt, Program: C:\Program Files\WindowsApps\Microsoft.WindowsNotepad_11.2501.31.0_x64__8wekyb3d8bbwe\Notepad\Notepad.exe
Extension: .pdf, Program: C:\Users\...\AppData\Local\Programs\Opera\opera.exe
Extension: .jpg, Program: None
Extension: .png, Program: None
Extension: .pptx, Program: C:\Program Files\Microsoft Office\Root\Office16\POWERPNT.EXE
Extension: .py, Program: C:\Users\...\AppData\Local\Programs\Python\Launcher\py.exe
Extension: .svg, Program: C:\Program Files\Inkscape\bin\inkscape.exe
Extension: .php, Program: C:\Users\...\AppData\Local\Programs\Microsoft VS Code\Code.exe
RESULTAT (logs):
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
DEBUG: UserChoice lookup failed for .jpg: [WinError 2] Le fichier spécifié est introuvable
DEBUG: HKEY_CLASSES_ROOT lookup failed for .jpg: [WinError 2] Le fichier spécifié est introuvable
DEBUG: HKEY_LOCAL_MACHINE lookup failed for .jpg: [WinError 2] Le fichier spécifié est introuvable
DEBUG: OpenWithProgids lookup failed for .jpg: [WinError 2] Le fichier spécifié est introuvable
DEBUG: OpenWithList lookup failed for .jpg: [WinError 2] Le fichier spécifié est introuvable
DEBUG: UserChoice lookup failed for .png: [WinError 2] Le fichier spécifié est introuvable
DEBUG: HKEY_CLASSES_ROOT lookup failed for .png: [WinError 2] Le fichier spécifié est introuvable
DEBUG: HKEY_LOCAL_MACHINE lookup failed for .png: [WinError 2] Le fichier spécifié est introuvable
DEBUG: OpenWithProgids lookup failed for .png: [WinError 2] Le fichier spécifié est introuvable
DEBUG: OpenWithList lookup failed for .png: [WinError 2] Le fichier spécifié est introuvable
DEBUG: UserChoice lookup failed for .pptx: [WinError 2] Le fichier spécifié est introuvable
DEBUG: UserChoice lookup failed for .py: [WinError 2] Le fichier spécifié est introuvable
DEBUG: UserChoice lookup failed for .php: [WinError 2] Le fichier spécifié est introuvable
DEBUG: HKEY_LOCAL_MACHINE lookup failed for .php: [WinError 2] Le fichier spécifié est introuvable
IMPORTANT:
Le code ci-dessus provient de diverses sources (autres forums, IA génératives, etc...), elles ont été bien pratiques mais malheureusement insuffisantes, d'où ce post.

Merci par avance de vos réponses

Dymon