``fileinput`` 模块允许你循环一个或多个文本文件的内容, 如 [Example 2-1 #eg-2-1] 所示.

====Example 2-1. 使用 fileinput 模块循环一个文本文件====[eg-2-1]

```
File: fileinput-example-1.py import fileinput
import sys for line in fileinput.input("samples/sample.txt"):
sys.stdout.write("-> ")
sys.stdout.write(line) *B*-> We will perhaps eventually be writing only small
-> modules which are identified by name as they are
-> used to build larger ones, so that devices like
-> indentation, rather than delimiters, might become
-> feasible for expressing local structure in the
-> source language.
-> -- Donald E. Knuth, December 1974*b*
``` 你也可以使用 ``fileinput`` 模块获得当前行的元信息 (meta information). 其中包括 ``isfirstline`` ,
``filename`` , ``lineno`` , 如 [Example 2-2 #eg-2-2] 所示. ====Example 2-2. 使用 fileinput 模块处理多个文本文件====[eg-2-2] ```
File: fileinput-example-2.py import fileinput
import glob
import string, sys for line in fileinput.input(glob.glob("samples/*.txt")):
if fileinput.isfirstline(): # first in a file?
sys.stderr.write("-- reading %s --\n" % fileinput.filename())
sys.stdout.write(str(fileinput.lineno()) + " " + string.upper(line)) *B*-- reading samples\sample.txt --
1 WE WILL PERHAPS EVENTUALLY BE WRITING ONLY SMALL
2 MODULES WHICH ARE IDENTIFIED BY NAME AS THEY ARE
3 USED TO BUILD LARGER ONES, SO THAT DEVICES LIKE
4 INDENTATION, RATHER THAN DELIMITERS, MIGHT BECOME
5 FEASIBLE FOR EXPRESSING LOCAL STRUCTURE IN THE
6 SOURCE LANGUAGE.
7 -- DONALD E. KNUTH, DECEMBER 1974*b*
``` 文本文件的替换操作很简单. 只需要把 ``inplace`` 关键字参数设置为 1 ,
传递给 ``input`` 函数, 该模块会帮你做好一切. [Example 2-3 #eg-2-3] 展示了这些. ====Example 2-3. 使用 fileinput 模块将 CRLF 改为 LF====[eg-2-3] ```
File: fileinput-example-3.py import fileinput, sys for line in fileinput.input(inplace=1):
# convert Windows/DOS text files to Unix files
if line[-2:] == "\r\n":
line = line[:-2] + "\n"
sys.stdout.write(line)
```

python标准库介绍——15 fileinput 模块详解的更多相关文章

  1. python标准库介绍——12 time 模块详解

    ==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...

  2. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  3. python标准库介绍——10 sys 模块详解

    ==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...

  4. python标准库介绍——30 code 模块详解

    ==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...

  5. python标准库介绍——19 mmap 模块详解

    ==mmap 模块== (2.0 新增) ``mmap`` 模块提供了操作系统内存映射函数的接口, 如 [Example 2-13 #eg-2-13] 所示. 映射区域的行为和字符串对象类似, 但数据 ...

  6. python标准库介绍——8 operator 模块详解

    ==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...

  7. python标准库介绍——5 re模块详解

    == re 模块== "Some people, when confronted with a problem, think 'I know, I'll use regular expres ...

  8. python标准库介绍——36 popen2 模块详解

    ==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...

  9. python标准库介绍——23 UserString 模块详解

    ==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...

随机推荐

  1. HP Onboard Administrator 固件升级

    HP Onboard Administrator是HP公司服务器的远程管理平台.更新是一个非常简单的过程,可以完全通过办公自动化web管理界面. 1. 下载所需二进制文件 下载地址:HP BladeS ...

  2. window系统下调度数据库类型资源库中的kettle job

    已经存在kettle的一个资源库enfo,在目录/works/wxj下面有一个job (testmailsuccess.kjb)如何实现手工在kettle外部执行此job和让系统每天定时的调用此job ...

  3. Transformer中引用iqd作为数据源的时提示TR1008无法连接问题

    刚接触tr的同行们可能都会遇到这么一个问题,当然我也遇到了. 今天把问题的解决办法详细的说明一下 从问题的表面来看是从cognos.ini文件缺少了什么配置东西,但是cognos10的安装中没有cog ...

  4. IOS遇到的问题总结

    1.NSString *path = [[NSBundle mainBundle] pathForResource:@"desc" ofType @"plist" ...

  5. WebService 之 已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性。

    在使用 WCF 中,遇到如下问题: 已超过传入消息(65536)的最大消息大小配额.若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性. 问题说明: 客户端调用 ...

  6. Oracle多行记录合并自定义函数

    在oracle数据库中,进行字段合并,可以使用wm_concat(column)函数,但是在这种方法不被Oracle所推荐,因为WMSYS用户用于Workspace Manager,其函数对象可能因版 ...

  7. WPF使用DEV之TreeListControl---加入复选框

    1.绑定的Class中加入一个Bool类型的属性 2.   该属性无需初始赋值,你执行就可以看到如图: Bool类型数据自己主动变成了复选框 watermark/2/text/aHR0cDovL2Js ...

  8. oracel become INDEX UNUSABLE

    1. IMPORT PARTITION or conventional path SQL*Loader. 2. Direct-path SQL*Loader leaves affected local ...

  9. Android 英文文档下载地址

    通过英文Android API学习Android技术是一个不错选择,当然养鸡的专业户要小心了,以下分享一些下载英文文档的链接(请使用迅雷下载): https://dl-ssl.google.com/a ...

  10. navicat ora-28547:connection to server failed

      navicat ora-28547:connection to server failed CreationTime--2018年8月9日18点47分 Author:Marydon 1.情景还原 ...