Tuesday, February 7, 2017

My Python Will Not Take a Command Line Argument

I had trouble getting my Python script to take a command line argument inside a subprocess.Popen call.   I do not mean that I had trouble with something like "python my_script.py arg".  No.  Here is what was failing for me:

import subprocess
proc = subprocess.Popen(‘executable_file.bin arg’)
proc.communicate()

What I got was "file not found" errors.  The Popen call could not recognize the argument as an argument.  It thought it was part of the file name.

I could run the command from a command line in a terminal.
executable_file.bin arg
worked in a command line.  Well, there is a way to make Popen work like a command line in a terminal/shell.  This is what works.

 subprocess.Popen(‘executable_file.bin arg’, shell=True)

The “shell=True” makes ‘executable_file.bin arg’work like it would in a terminal shell.

Robert

No comments:

Post a Comment

Comments require my approval before they appear. Be patient. It will take a day or two for your comment to appear.