1 使用sys.stdin 读取标准输入
[root@c6-ansible-20 script]# cat demo02.py
#! /usr/bin/env python
from __future__ import print_function
import sys
for line in sys.stdin:
print(line,end="")
使用方法:
cat /etc/passwd|python demo02.py
python demo02.py </etc/passwd
2 fileinput 读取标准输入
[root@c6-ansible-20 script]# cat demo04.py
#! /usr/bin/env python
from __future__ import print_function
import fileinput
for line in fileinput.input():
print(line,end='')
使用方法
python demo04.py /etc/passwd /etc/group
[root@c6-ansible-20 script]# cat demo02.py
#! /usr/bin/env python
from __future__ import print_function
import sys
for line in sys.stdin:
print(line,end="")
使用方法:
cat /etc/passwd|python demo02.py
python demo02.py </etc/passwd
2 fileinput 读取标准输入
[root@c6-ansible-20 script]# cat demo04.py
#! /usr/bin/env python
from __future__ import print_function
import fileinput
for line in fileinput.input():
print(line,end='')
使用方法
python demo04.py /etc/passwd /etc/group