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!

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
Depends on what is in 'args2' (if its a variable, for example).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.