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

前端 未结 13 1606
刺人心
刺人心 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:14

    I prefer:

    os.startfile(path, 'open')
    

    Note that this module supports filenames that have spaces in their folders and files e.g.

    A:\abc\folder with spaces\file with-spaces.txt
    

    (python docs) 'open' does not have to be added (it is the default). The docs specifically mention that this is like double-clicking on a file's icon in Windows Explorer.

    This solution is windows only.

提交回复
热议问题