I try to run a .bat
file in Windows using Python script.
我尝试使用Python脚本在Windows中运行.bat文件。
ask.bat
file:
ask.bat文件:
Application.exe work.xml
I write Python code :
我写Python代码:
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.
任何人都可以建议我如何让脚本运行。
7 个解决方案
#1
26
This has already been answered in detail on SO. Check out this thread, It should answer all your questions: Executing a subprocess fails
这已经在SO上得到了详细的回答。看看这个帖子,它应该回答你所有的问题:执行子进程失败
I've tried it myself with this code:
我用这段代码自己尝试过:
batchtest.py
batchtest.py
from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()
batch.bat
batch.bat
echo Hello World!
pause
I've got the batchtest.py example from the aforementioned thread.
我从前面提到的线程获得了batchtest.py示例。
#2
18
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
#3
4
Replace \ with / in the path
将\替换为路径中的/
import os
os.system("D:/xxx1/xxx2XMLnew/otr.bat ")
#4
4
-
It is better to write
.bat
file in such way that its running is not dependent on current working directory, i.e. I recommend to put this line at the beginning of.bat
file:最好以这样的方式编写.bat文件,使其运行不依赖于当前工作目录,即我建议将此行放在.bat文件的开头:
cd "%~dp0"
-
Enclose filepath of
.bat
file in double quotes, i.e.:用双引号括起.bat文件的文件路径,即:
os.system('"D:\\x\\so here can be spaces\\otr.bat" ["<arg0>" ["<arg1>" ...]]')
-
To save output of some batch command in another file you can use usual redirection syntax, for example:
要将某些批处理命令的输出保存在另一个文件中,您可以使用通常的重定向语法,例如:
os.system('"...bat" > outputfilename.txt')
Or directly in your
.bat
file:或直接在.bat文件中:
Application.exe work.xml > outputfilename.txt
#5
2
Probably the simplest way to do this is ->
可能最简单的方法是 - >
import os
os.chdir("X:\Enter location of .bat file")
os.startfile("ask.bat")
#6
0
If you are trying to call another exe
file inside the bat
-file. You must use SET Path
inside the bat
-file that you are calling. set Path should point into the directory there the exe
-file is located:
如果您尝试在bat文件中调用另一个exe文件。您必须在要调用的bat文件中使用SET Path。 set Path应该指向exe文件所在的目录:
set PATH=C:\;C:\DOS {Sets C:\;C:\DOS as the current search path.}
#7
0
python_test.py
python_test.py
import subprocess
a = subprocess.check_output("batch_1.bat")
print a
This gives output from batch file to be print on the python IDLE/running console. So in batch file you can echo the result in each step to debug the issue. This is also useful in automation when there is an error happening in the batch call, to understand and locate the error easily.(put "echo off" in batch file beginning to avoid printing everything)
这使得批处理文件的输出可以在python IDLE /运行控制台上打印。因此,在批处理文件中,您可以在每个步骤中回显结果以调试问题。当批量调用中发生错误时,这对于自动化也很有用,可以轻松地理解和定位错误。(在批处理文件中将“echo off”设置为开始以避免打印所有内容)
batch_1.bat
batch_1.bat
echo off
echo "Hello World"
md newdir
echo "made new directory"
#1
26
This has already been answered in detail on SO. Check out this thread, It should answer all your questions: Executing a subprocess fails
这已经在SO上得到了详细的回答。看看这个帖子,它应该回答你所有的问题:执行子进程失败
I've tried it myself with this code:
我用这段代码自己尝试过:
batchtest.py
batchtest.py
from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()
batch.bat
batch.bat
echo Hello World!
pause
I've got the batchtest.py example from the aforementioned thread.
我从前面提到的线程获得了batchtest.py示例。
#2
18
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
#3
4
Replace \ with / in the path
将\替换为路径中的/
import os
os.system("D:/xxx1/xxx2XMLnew/otr.bat ")
#4
4
-
It is better to write
.bat
file in such way that its running is not dependent on current working directory, i.e. I recommend to put this line at the beginning of.bat
file:最好以这样的方式编写.bat文件,使其运行不依赖于当前工作目录,即我建议将此行放在.bat文件的开头:
cd "%~dp0"
-
Enclose filepath of
.bat
file in double quotes, i.e.:用双引号括起.bat文件的文件路径,即:
os.system('"D:\\x\\so here can be spaces\\otr.bat" ["<arg0>" ["<arg1>" ...]]')
-
To save output of some batch command in another file you can use usual redirection syntax, for example:
要将某些批处理命令的输出保存在另一个文件中,您可以使用通常的重定向语法,例如:
os.system('"...bat" > outputfilename.txt')
Or directly in your
.bat
file:或直接在.bat文件中:
Application.exe work.xml > outputfilename.txt
#5
2
Probably the simplest way to do this is ->
可能最简单的方法是 - >
import os
os.chdir("X:\Enter location of .bat file")
os.startfile("ask.bat")
#6
0
If you are trying to call another exe
file inside the bat
-file. You must use SET Path
inside the bat
-file that you are calling. set Path should point into the directory there the exe
-file is located:
如果您尝试在bat文件中调用另一个exe文件。您必须在要调用的bat文件中使用SET Path。 set Path应该指向exe文件所在的目录:
set PATH=C:\;C:\DOS {Sets C:\;C:\DOS as the current search path.}
#7
0
python_test.py
python_test.py
import subprocess
a = subprocess.check_output("batch_1.bat")
print a
This gives output from batch file to be print on the python IDLE/running console. So in batch file you can echo the result in each step to debug the issue. This is also useful in automation when there is an error happening in the batch call, to understand and locate the error easily.(put "echo off" in batch file beginning to avoid printing everything)
这使得批处理文件的输出可以在python IDLE /运行控制台上打印。因此,在批处理文件中,您可以在每个步骤中回显结果以调试问题。当批量调用中发生错误时,这对于自动化也很有用,可以轻松地理解和定位错误。(在批处理文件中将“echo off”设置为开始以避免打印所有内容)
batch_1.bat
batch_1.bat
echo off
echo "Hello World"
md newdir
echo "made new directory"