python 使用sys.stdin和fileinput读入标准输入的方法

yipeiwu_com6年前Python基础

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

以上这篇python 使用sys.stdin和fileinput读入标准输入的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python正则表达式介绍

Python正则表达式介绍

注意:本文基于Python2.4完成;如果看到不明白的词汇请记得百度谷歌或维基,whatever。 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分。正则表...

Python循环语句中else的用法总结

前言 本文讨论Python的for…else和while…else等语法,这些是Python中最不常用、最为误解的语法特性之一。 Python中的for、while等循环都有一个可选的e...

Python中创建字典的几种方法总结(推荐)

1、传统的文字表达式: >>> d={'name':'Allen','age':21,'gender':'male'} >>> d {'age':...

Python在Matplotlib图中显示中文字体的操作方法

1.    说明 本篇主要针对在Ubuntu系统中,matplotlib显示不了中文的问题,尤其是在无法安装系统字体的情况下,解决Python绘图时中文显示...

pandas删除指定行详解

pandas删除指定行详解

在处理pandas的DataFrame中,如果想像excel那样筛选,只要其中的某一行或者几行,可以使用isin()方法来实现,只需要将需要的行值以列表方式传入即可,还可传入字典,进行指...