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

前端 未结 13 1318
刺人心
刺人心 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条回答
  •  -上瘾入骨i
    2020-11-22 11:11

    Start does not support long path names and white spaces. You have to convert it to 8.3 compatible paths.

    import subprocess
    import win32api
    
    filename = "C:\\Documents and Settings\\user\\Desktop\file.avi"
    filename_short = win32api.GetShortPathName(filename)
    
    subprocess.Popen('start ' + filename_short, shell=True )
    

    The file has to exist in order to work with the API call.

提交回复
热议问题