When using subprocess.Popen
, we have to write
with subprocess.Popen(['ls', '-l', '-a'], stdout=subprocess.PIPE) as proc:
print(proc.stdout.read())
instead of
with subprocess.Popen(['ls', '-l -a'], stdout=subprocess.PIPE) as proc:
print(proc.stdout.read())
Why? What ls
will get in the second case? Thank you.
If you want to use string representation of command to execute, shlex module may be useful.
It also covers more complex cases like escaping chars or quotes usage: