通过内置的fileinput模块即可实现,创建文件filein.py:
#!/usr/bin/env python import fileinput f_input = fileinput.input() for line in f_input: print(line, end='')
增加可执行权限:
chmod a+x filein.py
使用:
$ ls | ./filein.py # Prints a directory listing to stdout. $ ./filein.py /etc/passwd # Reads /etc/passwd to stdout. $ ./filein.py < /etc/passwd # Reads /etc/passwd to stdout.