4 ms·
Python isn't that readable when piping. From https://docs.python.org/3/library/subprocess.html#replacing-shell-pipeline https://docs.python.org/3/library/subpro
by sillysaurusx 6y ago
Python isn't that readable when piping. From https://docs.python.org/3/library/subprocess.html#replacing-shell-pipeline https://docs.python.org/3/library/subprocess.html#replacing-...
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]
This isn't a contrived example. One reason I like using macros in Python is because it simplifies exactly this boilerplate.
You can write a simplifying function, but programs rarely do.
- powersnail 6y agoI agree. Subprocess isn't really a shell-ish pipe. And it's sometimes slow (at least it used to be). This is why I use the shell overall, but when I need to process something that's better expressed in python, I use pypyp.
- sillysaurusx 6y agoThanks for mentioning pypyp! It looks sweet.
- d0mine 6y agoWhy would you reimplement piping? Just use the shell: output = check_output(“dmesg | grep hda”, shell=True) Or regular Python string methods could be used instead of piping to grep.
- deleted 6y ago[deleted]
- deleted 6y ago[deleted]