Open document with default OS application in Python, both in Windows and Mac OS

前端 未结 13 1322
刺人心
刺人心 2020-11-22 10:36

I need to be able to open a document using its default application in Windows and Mac OS. Basically, I want to do the same thing that happens when you double-click on the do

13条回答
  •  太阳男子
    2020-11-22 11:03

    On windows 8.1, below have worked while other given ways with subprocess.call fails with path has spaces in it.

    subprocess.call('cmd /c start "" "any file path with spaces"')
    

    By utilizing this and other's answers before, here's an inline code which works on multiple platforms.

    import sys, os, subprocess
    subprocess.call(('cmd /c start "" "'+ filepath +'"') if os.name is 'nt' else ('open' if sys.platform.startswith('darwin') else 'xdg-open', filepath))
    

提交回复
热议问题