我喜欢这样做。
导入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)
答案 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()