1

I'd like to load cmd.exe and call a bat file with multiple parameters. All these from python.

My bat file looks like:

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

My python code:

os.system("start cmd.exe /k C:/Users/username/desktop/file.bat" params)

where:

params = [JobName, fname, str(numcpus), '1', 'C:/Abaqus_Jobs' + JobDir]

JobName = 'A2'
JobDir = 'Job1/input_files'
fnameUEL = 'routineUEL'
numcpus = 8

Now this code does not work because params is a list. If I change it to:

os.system("start cmd.exe /k C:/Users/username/desktop/file.bat JobName  fname  str(numcpus)  '1'  'C:/Abaqus_Jobs' + JobDir)

it also does not work. I've read that cmd in windows works:

Spaces in Program Path + parameters with spaces:
CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""

but if I try:

os.system("start cmd.exe /k ""C:/Users/username/desktop/file.bat" "JobName"  "fname" "str(numcpus)"  "1"  "'C:/Abaqus_Jobs' + JobDir"")

it doesn't work either. Any ideas? Thanks!

jpcgandre
  • 1,487
  • 5
  • 31
  • 55
  • look here this may help http://stackoverflow.com/questions/1818774/executing-a-subprocess-fails – Hank Lapidez Jul 11 '14 at 17:23
  • @HankLapidez: I've tried with `Popen` and it works but no cmd window shows up and I need this to see possible errors. – jpcgandre Jul 11 '14 at 17:27
  • If you use popen you can pipe stdout/stderr back to the python program. Not sure why would would need to see the cmd window. – Bob Jul 11 '14 at 17:29
  • read stdout and stderr. http://stackoverflow.com/questions/2804543/read-subprocess-stdout-line-by-line – Hank Lapidez Jul 11 '14 at 17:30
  • The thing is I'm using my python script as an external script of another program. It then uses my python script so I don't see the python terminal when the program is running. – jpcgandre Jul 11 '14 at 17:31

1 Answers1

1

Solved it with:

process = Popen(params , stdout=PIPE, stderr=PIPE, shell=True)

params = [['cmd', '/k', 'start', ....]]
jpcgandre
  • 1,487
  • 5
  • 31
  • 55