我有这个代码,我需要运行一个子进程,并在命令窗口中打印出什么输出。
import subprocess
msprompt = 'C:\Windows\Microsoft.NET\Framework64\\v4.0.30319\\MSBuild.exe'
path = "C:/Users/bgb/Documents/Brent/Code/Visual Studio/tree.DataManagement.UnitTests./tree.DataManagement.UnitTests.vbproj"
def command(msprompt, openFile):
for line in finalPathList:
p = subprocess.Popen([msprompt, path], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
print line,
retval = p.wait()
当我在编译器中运行它时它不起作用,它会吐出这条消息:
MSBUILD : error MSB1009: Project file does not exist.
Switch: "C:/Users/bgb/Documents/Brent/Code/Visual Studio/tree.FormControls.UnitTests./tree.FormControls.UnitTests.vbproj"
但是,如果我打开命令窗口完全独立,我复制并粘贴msprompt
然后我复制path
并将其粘贴到命令窗口并点击回车,它完美地运行,有谁知道我在command
函数搞乱了什么?
非常感谢帮助!
答案 0 :(得分:1)
我认为您的路径格式不正确,应该使用两个反斜杠\\
而不是正斜杠/
。
出于调试目的,请尝试使用os.path.exists(path)
,首先确保路径正确。然后,您可以使用os.path.join
修复路径(文档here)。