How do I execute a program from Python? os.system fails due to spaces in path

后端 未结 10 886
一生所求
一生所求 2020-11-22 08:19

I have a Python script that needs to execute an external program, but for some reason fails.

If I have the following script:

import os;
os.system(\"C         


        
10条回答
  •  有刺的猬
    2020-11-22 09:14

    Here's a different way of doing it.

    If you're using Windows the following acts like double-clicking the file in Explorer, or giving the file name as an argument to the DOS "start" command: the file is opened with whatever application (if any) its extension is associated with.

    filepath = 'textfile.txt'
    import os
    os.startfile(filepath)
    

    Example:

    import os
    os.startfile('textfile.txt')
    

    This will open textfile.txt with Notepad if Notepad is associated with .txt files.

提交回复
热议问题