使用python code运行.bat文件 [英] Run a .bat file using python code

查看:2398
本文介绍了使用python code运行.bat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Python脚本来运行Windows中的的.bat 文件。

ask.bat 文件:

  Application.exe work.xml

我编写Python code:

 导入OS
使用os.system(D:\\ XXX1 \\ xxx2XMLnew \\ otr.bat)

输出:当尝试运行该文件它只是给命令提示符下的眨眼,而且工作不执行

注:我尝试用备用斜线还可以,但它不工作

和我也想保存文件的输出在另一个文件中。

任何人都可以建议我怎么才能使脚本运行的。


解决方案

 导入子文件路径=D:/path/to/batch/myBatch.bat
P = subprocess.Popen(文件路径,壳= TRUE,标准输出= subprocess.PIPE)标准输出,标准错误= p.communicate()
打印p.return code#是0​​,如果成功

I try to run a .bat file in Windows using Python script.

ask.bat file:

Application.exe work.xml

I write Python code :

import os
os.system("D:\xxx1\xxx2XMLnew\otr.bat ")

Output: when try to run the file its just give a blink of the command prompt, and the work is not performing.

Note: I try with alternate slash also , but it is not working.

And I also want to save output of the file in another file.

Can anyone suggest how can I make the script runnable.

解决方案

import subprocess

filepath="D:/path/to/batch/myBatch.bat"
p = subprocess.Popen(filepath, shell=True, stdout = subprocess.PIPE)

stdout, stderr = p.communicate()
print p.returncode # is 0 if success

这篇关于使用python code运行.bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆