如何从 Python 执行 VS2008 命令并获取其输出?
我希望
tf changeset 12345
使用 Visual Studio 2008 命令工具运行。它位于:"c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\"
,启动的命令是:%comspec% /k ""c: \Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"" x86
我想以某种方式将“tf 变更集 12345”附加到它并将其保存到字符串中,而不首先将其重定向到文件。我注意到,当我简单地从命令行调用它时,当我输入:时,我会得到 GUI:
tf changeset 12345
当我这样做时,我会得到文本输出:
tf changeset 12345 > out.txt
我不喜欢在文件系统上创建文件,但希望只是在“ Python式的方式”。
我看过 os.system()、子进程的简短示例,但它们似乎都没有说明如何做我想做的事情:
- 从特定目录运行进程(最好不使用 chdir)
- 执行包含环境变量的命令+ 自定义文本。
- 重定向输出而不创建临时文件。
希望你能帮助我接近我想要的。如果您在 VS2008 或其他一些 Windows 程序上测试该解决方案,将会有所帮助。
谢谢你!
I wish to run
tf changeset 12345
Using the Visual Studio 2008 Command tool. It is located in: "c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\"
and the command that gets launched is: %comspec% /k ""c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"" x86
I would like to append the "tf changeset 12345" to it somehow and save it to a string WITHOUT first redirecting it to a file. I noticed that when I simply call it from the command line, I get GUI when I type:
tf changeset 12345
and I get the textual output when I do:
tf changeset 12345 > out.txt
I prefer not to create a file on the file system, but hopefully just read it in the "Pythonic way".
I have seen brief examples of os.system(), subprocess, but none of them seem to illustrate how to do what I want to do:
- Run the process from a particular directory (preferably without using chdir)
- Executing a command which contains environment variables + custom text.
- Redirect the output without creating a temporary file.
Hopefully you can help me get close to what I want. It would help if you tested the solution on VS2008 or some other Windows program.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看我在此处使用的代码您正在寻找的工作。这是用C#编写的,是一个灵活的类,你只需要更改命令,查看
ps.FileName
和ps.Arguments
,命令的输出得到如果需要,重定向到 StringBuilder 实例进行解析。在窗口中执行命令的 shell 是完全隐藏的,不显示。希望这有帮助,
此致,
汤姆.
Have a look at this code I used here to do the job that you're looking for. This is written in C#, and is a flexible class, you just need to change the command, look in
ps.FileName
andps.Arguments
, the output of the command gets redirected to aStringBuilder
instance for parsing if so required. The shell executing command in a window is completely hidden and does not show.Hope this helps,
Best regards,
Tom.