使用子进程使Windows CMD脚本工作。

时间:2018-01-22 16:34:50

标签: python popen

我喜欢这样做。     导入subprocess

p = subprocess.Popen(["@ECHO ON,", "SET TmpBatch=%temp%\~tmpLogonMessage.cmd,",
'IF EXIST "%TmpBatch%" DEL /Q /F "%TmpBatch%',  
"SET msgboxTitle=This is my Message Title,",
"SET msgboxLine1=This is my temp Message Window that pops up at Windows Logon,",
"SET tmpmsgbox=%temp%\~tmpmsgbox.vbs,",
'ECHO @ECHO OFF                                                    >>"%TmpBatch%",',
'ECHO IF EXIST "%tmpmsgbox%" DEL /F /Q "%tmpmsgbox%"               >>"%TmpBatch%",',
'ECHO ECHO msgbox "%msgboxLine1%",0,"%msgboxTitle%"^>"%tmpmsgbox%" >>"%TmpBatch%",',
'ECHO WSCRIPT "%tmpmsgbox%"                                        >>"%TmpBatch%",',
'START /MIN CMD /C "%TmpBatch%",', 'EXIT /B,'], stdout=subprocess.PIPE)

1 个答案:

答案 0 :(得分:0)

最好将所有脚本内容放在bat文件中,然后使用 subprocess.Popen 执行它:

EX:

import subprocess
filepath = "C:/path/to/bat/myBat.bat"
p = subprocess.Popen(filepath, shell=True, stdout = subprocess.PIPE)
stdout, stderr = p.communicate()