-1

I am looking for an example of a python/batch script:

  • python script calls up the command line
  • python script passes a batch script (several lines) to the command line
  • batch script then executes in the command line
  • python script recieves the result of the batch script

I assume the solution would depend on some kind of special python library?

Would the task of combining python/batch be much easier if the processes starts in batch rather than in python (would this be a more common solution)?

whatIS
  • 57
  • 6

2 Answers2

0

See the subprocess module, which is included in the standard library:

http://docs.python.org/2/library/subprocess.html

There are lots of examples there.

BenDundee
  • 4,389
  • 3
  • 28
  • 34
0

Code example:

import subprocess
bat_text = "dir" # (text to write in bat file)
bat_file = open("program.bat","w") # (open file in write mode)
bat_file.write(bat_text) # (write bat_text on bat_file)
bat_file.close() # (close file)
command = "C:\program.bat" # (the location of bat file)
execute = subprocess.Popen(command, stdout=subprocess.PIPE)  # (exec the command)
output = execute.stdout.read() # (read output)
print output  # (print output)

subprocess tutorial

thc-scripting
  • 76
  • 1
  • 4