I want to create a simple “virtual bash” script using Python, that takes commands in and returns the stdio output. Something like this:
>>> output = bash('ls')
>>> print output
file1 file2 file3
>>> print bash('cat file4')
cat: file4: No such file or directory
Does anyone know a module/function that allows this to happen? I could not find one.
The
subprocessmodule holds the answers to all your problems. In particular,check_outputseems to do exactly what you want. Examples from the page: