在使用popen调用bat文件时显示cmd窗口

0 投票
2 回答
1070 浏览
提问于 2025-04-18 12:53

我有一段代码,它用popen来调用一个bat文件:

jobname = A2
fname = routineUEL

 def issue_command(*args):
      import subprocess
      process = subprocess.popen(args, stdout=subprocess.pipe, stderr = subprocess.pipe, shell = true)

  args = [['c:\users\desktop\\rel.bat', jobname, fname, '8', '1', ' c:\abaqus_jobs']]

  for arg in args:
       out, err = issue_command(*arg)

这个bat文件是:

  @echo off
  call ifortvars.bat intel64
  cd %5
  abaqus job=#1 user=%2 cpus=%3 gpus=%4 interactive

问题是:当我运行这个python脚本时,bat文件里的命令是在后台运行的,没有打开命令提示符窗口。而当我手动运行这个bat文件时,命令提示符窗口会打开。我希望在用python运行时也能打开这个命令提示符窗口。有什么办法吗?谢谢!

2 个回答

0

@jpcgandre,在你提到的辅助批处理文件中,里面只有那一行代码吗?还是说还有其他内容?你是想把那一行添加到rel.bat文件里吗?能不能再具体一点?这里有一个解决方案:

with open("rel.bat","a") as text_file:
    print("cmd.exe /K 'c:\\users\\desktop\\rel.bat'", file=text_file)
1

如果你想让命令窗口出现,可以通过运行 cmd.exe 来实现。

你可以输入 cmd.exe /K 'c:\\users\\desktop\\rel.bat'

/K 是在告诉 cmd.exe 执行这个批处理文件,并且保持窗口打开。

撰写回答