ITPub博客

首页 > IT职业 > IT职场 > python 如何获得重定向输入

python 如何获得重定向输入

IT职场 作者:mocow 时间:2021-09-11 10:32:50 0 删除 编辑

通过内置的fileinput模块即可实现,创建文件filein.py:

#!/usr/bin/env python
import fileinput
f_input = fileinput.input()
for line in f_input:
     print(line, end='')

相关推荐:《Python教程》

增加可执行权限:

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.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2815/viewspace-2836503/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-17

  • 博文量
    108
  • 访问量
    1363