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

后端 未结 10 879
一生所求
一生所求 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:12

    The outermost quotes are consumed by Python itself, and the Windows shell doesn't see it. As mentioned above, Windows only understands double-quotes. Python will convert forward-slashed to backslashes on Windows, so you can use

    os.system('"C://Temp/a b c/Notepad.exe"')
    

    The ' is consumed by Python, which then passes "C://Temp/a b c/Notepad.exe" (as a Windows path, no double-backslashes needed) to CMD.EXE

提交回复
热议问题