如何使python脚本“可管道"猛击? [英] How to make a python script "pipeable" in bash?

查看:72
本文介绍了如何使python脚本“可管道"猛击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个脚本,希望它在bash中是 pipeable .像这样:

I wrote a script and I want it to be pipeable in bash. Something like:

echo "1stArg" | myscript.py

有可能吗?怎么样?

推荐答案

请参见以下简单的echo.py:

import sys

if __name__ == "__main__":
    for line in sys.stdin:
        sys.stderr.write("DEBUG: got line: " + line)
        sys.stdout.write(line)

运行:

ls | python echo.py 2>debug_output.txt | sort

输出:

echo.py
test.py
test.sh

debug_output.txt内容:

debug_output.txt content:

DEBUG: got line: echo.py
DEBUG: got line: test.py
DEBUG: got line: test.sh

这篇关于如何使python脚本“可管道"猛击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆