通过内置的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.
python学习网,大量的免费python视频教程,欢迎在线学习!