Search code examples
pythonsubprocess

How to run a program by default app in python


I need to start my app with python by defalut. Like double-clicking on them.

I know there is subprocess.Popen("program", "file") but I need to start app by default and I don't know with what program I need to start app.


Solution

  • for windows you can use os.startfile(FILEPATH) and for macOS you can use subprocess.call(('open', FILEPATH))

    and for all systems this code is useful:

    import platform
    import os
    
    if platform.system() == 'Darwin':       # macOS
        subprocess.call(('open', FILEPATH))
    elif platform.system() == 'Windows':    # Windows
        os.startfile(FILEPATH)
    else:                                   # linux variants
        subprocess.call(('xdg-open', FILEPATH))