简体   繁体   English

如何通过 python 运行批处理命令?

[英]How can I run batch commands through python?

So I have a batch file.所以我有一个批处理文件。 I want to add the batch commands from the batch file to my main python file.我想将批处理文件中的批处理命令添加到我的主 python 文件中。 How can I do that?我怎样才能做到这一点? Is there a pip module for that?有没有 pip 模块呢?

Here's the batch code I want to add to the main python file:这是我要添加到主 python 文件的批处理代码:

@ECHO OFF
title TOG6 Spoofs


SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

:menuLOOP

echo.--------------
echo.- TOG6 Spoofs -
echo.--------------
echo.
for /f "tokens=1,2,* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo.  %%B  %%C
set choice=
echo.&set /p choice= Please select your choice:  ||GOTO:EOF
echo.&call:menu_%choice%


::-----------------------------------------------------------
:: menu functions follow below here
::-----------------------------------------------------------

:menu_1   Spoof HDD Serial

SET CurrentDir=%~dp0
echo.Mapping Driver.
%CurrentDir%mapper.exe spoofer.sys
echo.Driver Mapped. HDD serial has been successfully changed.
pause
cls

Thanks: :-)谢谢: :-)

I think this post will help you but in short you'd do something like this:我认为这篇文章会对你有所帮助,但简而言之,你会做这样的事情:

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

If you wanted to get output from the batch file you can get that in the stdout.如果您想从批处理文件中获取 output,您可以在标准输出中获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM