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

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

    If you have to use an heuristic method, you may consider webbrowser.
    It's standard library and despite of its name it would also try to open files:

    Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program. However, this is neither supported nor portable. (Reference)

    I tried this code and it worked fine in Windows 7 and Ubuntu Natty:

    import webbrowser
    webbrowser.open("path_to_file")
    

    This code also works fine in Windows XP Professional, using Internet Explorer 8.

提交回复
热议问题