How to execute a UNIX command in Python script

python, unix

Solution

You cannot use UNIX commands in your Python script as if they were Python code, `echo name` is causing a syntax error because `echo` is not a built-in statement or function in Python. Instead, use `print name`.

To run UNIX commands you will need to create a subprocess that runs the command. The simplest way to do this is using `os.system()`, but the `subprocess` module is preferable.

Problem

``` #!/usr/bin/python import os import shutil import commands import time import copy name = 'test' echo name ``` I have a simple python scripts like the above. When I attempt to execute it I get a syntax error when trying to output the name variable.

Original source