-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)?

4

2 回答 2

0

代码示例:

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)

子流程教程

于 2013-01-15T23:17:46.317 回答
0

查看包含在标准库中的 subprocess 模块:

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

那里有很多例子。

于 2013-01-15T21:36:24.360 回答