从linux命令行读取Python

问题描述:

有没有办法读取进入命令行的数据,直接进入另一个Python脚本执行?

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

您需要从python脚本中阅读 stdin

You need to read stdin from the python script.

import sys

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

运行示例 -

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