3 ms·
It has always pretty easy for me to read from and write to pipes in python, assuming you are talking about /std(out|in|err)/. from sys import stdin, stdout,
by sprayk 13y ago
It has always pretty easy for me to read from and write to pipes in python, assuming you are talking about /std(out|in|err)/.
from sys import stdin, stdout, stderr
for n,line in enumerate(stdin):
stdout.write(str(n) + '\n')
stderr.write(line)
Is that the kind of reading and writing you are talking about?
- jjoonathan 13y agoI suspect it has more to do with stringing together the inputs/outputs of multiple programs, which is outright painful in python. The syntax is terribly verbose (import subprocess, use subprocess.call, refer to a few enum values in the subprocess package, wire them together which requires a line of code rather than a character). Worse, pipes are tricky to implement correctly. It's easy to deadlock the python process by overflowing a pipe's buffer (wait for exit to read the buffer, buffer fills before subprocess exit, deadlock), so people who don't have an intimate knowledge of the underlying implementation will find themselves stuck with a bug they can't fix. Compare this to a shell where the combination of &&, ||, and | can fit on a single line what would have taken dozens of lines of python.
- abraininavat 13y agoSee https://github.com/kennethreitz/envoy https://github.com/kennethreitz/envoy Example: >> r = envoy.run('uptime | pbcopy')
- sprayk 13y agoAh, so using python for stuff bash is usually used for. I thought OP meant writing python scripts to be used as components of bash scripts. For _that_ type of python, I usually use the sh module[0]. It handles piping pretty well, especially if you use StringIO to deal with the pipes. Unfortunately, return code-based conditionals are nowhere as simple as || and && are in shell. [0] http://amoffat.github.io/sh/ http://amoffat.github.io/sh/