Reading from linux command line with Python

前端 未结 3 1122
误落风尘
误落风尘 2021-01-16 09:52

Is there a way to read data that is coming into the command-line, straight into another Python script for execution?

3条回答
  •  耶瑟儿~
    2021-01-16 10:29

    You need to read stdin from the python script.

    import sys
    
    data = sys.stdin.read()
    print 'Data from stdin -', data
    

    Sample run -

    $ date | python test.py
    Data from stdin - Wed Jun 17 11:59:43 PDT 2015
    

提交回复
热议问题