2013-01-15 89 views
-1

我寻找一个python /批处理脚本的一个示例:调用批处理的python脚本示例,然后接收批处理脚本结果的python脚本?

  • Python脚本调出命令行
  • Python脚本通过一个批处理脚本(几行)到命令行
  • 批处理脚本然后执行命令行
  • python脚本临危批处理脚本的

我认为解决方案将取决于某种特殊的蟒蛇LIB的结果郭宝宏?

如果批处理而不是python(这将是一个更常见的解决方案)的开始将Python /批量组合的任务会更容易吗?

+0

你的意思'bash'吧? –

+2

请参阅http://stackoverflow.com/questions/706989/how-to-call-an-external-program-in-python-and-retrieve-the-output-and-return-cod –

回答

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) 

子教程

+1

谢谢!正是我在找的东西。如果我有超过15的声望,就会投票! – whatIS