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

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

问题描述

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

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

ask.bat 文件:

Application.exe work.xml

我编写 Python 代码:

I write Python code :

import os
os.system("D:xxx1xxx2XMLnewotr.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.

推荐答案

这个已经在 SO 上详细回答了.看看这个线程,它应该回答你所有的问题:执行子进程失败

This has already been answered in detail on SO. Check out this thread, It should answer all your questions: Executing a subprocess fails

我自己用这个代码试过了:

I've tried it myself with this code:

batchtest.py

from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:Path	oatchfolder")
stdout, stderr = p.communicate()

batch.bat

echo Hello World!
pause

我从上述线程中获得了 batchtest.py 示例.

I've got the batchtest.py example from the aforementioned thread.

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

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