1

I am trying to send arguments to a batch file.

I have a batch file, HelloWorld.bat and it asks for 4 inputs in total at various points in the script. I tried using subprocess.Popen, subprocess.call, and os.system but I haven't been able to pass the arguments in. This is what I've got so far:

p = subprocess.Popen(shlex.split("cmd"), shell = True, stdin = subprocess.PIPE)
p.communicate(
     "cd " + filepath + "\n" +
     "HelloWorld.bat\n" +

     "arg1\n" +
     "arg2\n" +
     "arg3\n" +
     "arg4\n"
 )

When I run this code it says that wrong command syntax. Is there anyway I can pass arguments to the batch file that will be running in cmd?

Thanks!

user3651858
  • 21
  • 1
  • 3

1 Answers1

6

You don't need shell=True for Windows, you can simply pass the arguments in a sequence, like this:

executable = os.path.join(filepath, 'HelloWorld.bat')
p = subprocess.Popen([executable, 'arg1', 'arg2', 'arg3', 'arg4'])

There is a lot of information at the documentation for the subprocess module, I suggest a good read through it as you can easily hose your system if you are not careful.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Thank you so much! Now I'm getting something. However, i am still having an issue passing the arguments, When I pass it in, it says "The syntax of the command is incorrect." Is there some formatting issue that I'm not catching? – user3651858 Jun 11 '14 at 08:35
  • Depends on what is in 'args2' (if its a variable, for example). – Burhan Khalid Jun 11 '14 at 08:37