How to execute VS2008 command with Python and capture its output?

I want to run

tf changeset 12345 

Using the Visual Studio 2008 command-line tool. It is located in: "c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\" , and the command that runs is the following: %comspec% /k ""c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"" x86

I would like to somehow add โ€œtf changeset 12345โ€ to it and save it in the line WITHOUT first redirecting it to a file. I noticed that when I just call it from the command line, I get a graphical interface as I type:

 tf changeset 12345 

and I get the text output when I do:

 tf changeset 12345 > out.txt 

I prefer not to create the file in the file system, but hopefully just read it in the "Pythonic way".

I saw brief examples of os.system (), a subprocess, but none of them show how to do what I want to do:

  • Run the process from a specific directory (preferably without using chdir)
  • Executing a command containing environment variables + user text.
  • Redirect output without creating a temporary file.

Hope you can help me get closer to what I want. This will help if you tested the solution on VS2008 or some other Windows program.

Thanks!

+4
source share
2 answers
 process = subprocess.Popen(['tf', 'changeset', '12345'], cwd='c:/somedir', env={'SOMEENVVAR': 'SOMEVALUE', ...}, stdout=subprocess.PIPE) for line in process.stdout: print line process.terminate() 
+3
source

Take a look at this code that I used here to complete the work you are looking for. This is written in C #, and is a flexible class, you just need to change the command, see <T20> and ps.Arguments , then the output of the command is redirected to StringBuilder , for example, to parse if necessary. The shell execution command in the window is completely hidden and is not displayed.

Hope this helps, Regards, Tom.

+2
source

Source: https://habr.com/ru/post/1301197/


All Articles