screen -ls | tail -n +2 | head -n -2 | awk '{print $1}'| xargs -I{} screen -S {} -X quit
subprocess.call('ls -la | grep i', shell=True)
>>> p = subprocess.Popen('ls -la|grep i', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> p
<subprocess.Popen object at 0x2173790>
>>> p.stdout
<_io.FileIO name=3 mode='rb'>
>>> p.stdout.read()
>>> p.stderr.read()
output=`dmesg | grep hda`
# becomes
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]
screen -ls | tail -n +2 | head -n -2 | awk '{print $1}'| xargs -I{} screen -S {} -X quit
sh (previously pbs) is a full-fledged subprocess interface for Python that allows you to call any program as if it were a functionamoffat.github.io/sh/#piping
Plumbum is a small yet feature-rich library for shell script-like programs in Pythonhttps://plumbum.readthedocs.org/en/latest/#cheat-sheet
p1 = Popen(['screen', '-la'], stdout=PIPE)
p2 = Popen(['tail', '-n', '+2'], stdin=p1.stdout, stdout=PIPE)
p3 = Popen(['head', '-n', '-2'], stdin=p2.stdout, stdout=PIPE)
p4 = Popen(['awk', '"{print $1}"'], stdin=p3.stdout, stdout=PIPE)
p5 = Popen(['xargs', '-I{}', 'screen', '-S', '{}', '-X', 'quit'], stdin=p4.stdout, stdout=PIPE)
p1.stdout.close()
p2.stdout.close()
p3.stdout.close()
p4.stdout.close()
output = p4.communicate[0];
print(output)