【问题标题】:How to receive arguments via shell pipe in python?如何通过python中的shell管道接收参数?
【发布时间】:2011-08-31 16:13:57
【问题描述】:

我想做这样的事情:

find -name "foo*" | python main.py

并访问 find 程序找到的所有文件。如何在 Python 中访问它?

【问题讨论】:

  • 运行了很长时间的命令呢,例如top

标签: python linux shell pipe


【解决方案1】:

我喜欢将 $(...) 用于依赖于其他程序的命令行参数。我认为这适用于您的程序python main.py $(find -name "foo*")。找到here

【讨论】:

    【解决方案2】:

    我相信fileinput 可能是你想要的。

    【讨论】:

      【解决方案3】:

      我认为你可以做到:

      import sys
      print sys.stdin.readlines() #or what you want
      

      【讨论】:

        【解决方案4】:
        import sys
        for line in sys.stdin:
            print line
        

        【讨论】:

          【解决方案5】:

          使用 sys.stdin.read() 或 raw_input()

          管道只是将标准输入文件描述符更改为指向左侧命令正在写入的管道。

          【讨论】:

            猜你喜欢
            • 2014-05-23
            • 1970-01-01
            • 2011-10-02
            • 2012-05-11
            • 1970-01-01
            • 2018-04-04
            • 1970-01-01
            • 1970-01-01
            • 2019-02-24
            相关资源
            最近更新 更多