Equivalent of Bash Backticks in Python

Chris Bunch picture Chris Bunch · Sep 11, 2009 · Viewed 43.6k times · Source

What is the equivalent of the backticks found in Ruby and Perl in Python? That is, in Ruby I can do this:

foo = `cat /tmp/baz`

What does the equivalent statement look like in Python? I've tried os.system("cat /tmp/baz") but that puts the result to standard out and returns to me the error code of that operation.

Answer

John Kugelman picture John Kugelman · Sep 11, 2009
output = os.popen('cat /tmp/baz').read()