Use the subprocess module
import subprocess p = subprocess.Popen("ls -l | grep %s | awk '{ print $9 }'" % mask, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate()
The shell=True is required as the pipeline is run by the shell, otherwise you get a No such file or directory.
In Python 2.7 you can also use
output = subprocess.check_output( "ls -l | grep %s | awk '{ print $9 }'" % mask stderr=subprocess.STDOUT, shell=True)
But I find it cumbersome to use as it throws a subprocess.CalledProcessError if the pipeline returns exit code other than 0, and to capture both stdout and stderr you need to interleave them both, which makes it unusable for many cases.
os.systemthey would have recommended that you usesubprocessinstead, and linked you straight to a section that shows exactly what you're trying to do. I don't know why you expect us to be able to explain it better than the documentation.