Passing arguments to shell script from Python
Question: I'm writing a python program which passes arguments to a shell script.

Here's my python code:

    
    
    import subprocess
    
    Process=subprocess.Popen('./copyImage.sh %s' %s str(myPic.jpg))
    

And my "copyImage.sh":

    
    
    #!/bin/sh
    
    cp /home/pi/project/$1 /home/pi/project/newImage.jpg
    

I can run the script on terminal without problems. But when executing the
python code, the terminal returned `"NameError: name 'myPic' is not defined"`.

If I change the syntax to

    
    
    Process=subprocess.Popen('./copyImage.sh %s' %s "myPic.jpg")
    

Then the terminal returned `"OSError: [Errno 2] No such file or directory"`.

I've followed this: [Python: executing shell script with arguments(variable),
but argument is not read in shell
script](http://stackoverflow.com/questions/19325761/python-executing-shell-
script-with-argumentsvariable-but-argument-is-not-rea) but it didn't help.


Answer: The `subprocess` module is expecting a list of arguments, not a space-
separated string. The way you tried caused python to look for a program called
`"copyImage.sh myPic.jpg"` and call it with no arguments, whereas you wanted
to look for a program called `copyImage.sh` and call it with one argument.

    
    
    subprocess.check_call(['copyImage.sh', 'myPic.jpg'])
    

I also want to mention, since your script simply calls copy in a shell, you
should probably cut out the middleman and just use python's
[`shutil.copy`](https://docs.python.org/2/library/shutil.html#shutil.copy)
directly. It's a more appropriate tool than running a subprocess for this
task.

