释译python fileinput中的description

简介:
fileinput 模块提供处理一个或多个文本文件的功能 可以通过使用 for..in 来循环读取一个或多个文本文件内容 .

例子中的文件,

1.txt
1a

2a
3a
4a

2.txt
1b
2b

 

DESCRIPTION

    Typical use is:

   

        import fileinput

        for line in fileinput.input():

            process(line)

   

    This iterates over the lines of all files listed in sys.argv[1:],

    defaulting to sys.stdin if the list is empty.  If a filename is '-' it

    is also replaced by sys.stdin.  To specify an alternative list of

    filenames, pass it as the argument to input().  A single file name is

    also allowed.

[ ] 这个迭代了所有文件的行在 sys.argv[1:] 中,如果列表为空则默认为标准输入,如果文件名为 ”-” 它也为标准输入。指定一个文件名列表来做为参数传递给 input ,一个单独的文件名也是允许的。

[ ]

(1)#!/usr/bin/env python

import fileinput,sys

 

for line in fileinput.input(sys.argv[1:]):

    pass

print fileinput.lineno(),

命令行下,输入 python test.py 1.txt 2.txt

(2)#!/usr/bin/env python

import fileinput

 

for line in fileinput.input(['1.txt','2.txt']):

    pass

print fileinput.lineno(),

(3) #!/usr/bin/env python

import fileinput

 

for line in fileinput.input(“1.txt”):

    pass

print fileinput.lineno(),

 

    Functions filename(), lineno() return the filename and cumulative line

    number of the line that has just been read; filelineno() returns its

    line number in the current file; isfirstline() returns true iff the

    line just read is the first line of its file; isstdin() returns true

    iff the line was read from sys.stdin.  Function nextfile() closes the

    current file so that the next iteration will read the first line from

    the next file (if any); lines not read from the file will not count

    towards the cumulative line count; the filename is not changed until

    after the first line of the next file has been read.  Function close()

    closes the sequence.

[ ] 函数 filename,lineno 返回的读取的文件名与已经读的累计的行数; filelineno 返回当前文件的行数的函数 ; 如果读的是它自己的文件第一行,那么 isfirstline 是正确的。如果读的是来自标准输入那么 isstdin 返回真。函数 nextfile 关闭当前文件以致下一个迭代器将从下一个文件第一行读起;将不会累计上一个文件的行数 . 这个文件名不会改变,直到读取下一个文件的第一行。函数 close 关闭序列。

[ ]

(1) #!/usr/bin/env python

import fileinput

 

for line in fileinput.input(['1.txt']):

    pass

print fileinput.filename(),fileinput.lineno()

[root@newpatch3 /home/python]#python test.py

1.txt 4

 

(2)    #!/usr/bin/env python

import fileinput   

 

for line in fileinput.input(['1.txt','2.txt']):

    pass

print fileinput.filename(),":",fileinput.filelineno()

print "1.txt and 2.txt total line:",fileinput.lineno()

[root@newpatch3 /home/python]#python test.py

2.txt : 2

1.txt and 2.txt total line: 6

大家看到没, filelineno lineno 的差异了吧?

(3) #!/usr/bin/env python

import fileinput,sys

 

for line in fileinput.input([‘1.txt’]):

    if fileinput.isfirstline():

        print line,

        sys.exit(0)

[root@newpatch3 /home/python]#python test.py

1a

1.txt 中有 1a,2a,3a,4a 四行数,但我们通过条件判断,只取第一条,来演示 isfirstline 功能 .

(4) #!/usr/bin/env python

import fileinput

 

for line in fileinput.input():

    if fileinput.isstdin():

        print "isstdin"

[root@newpatch3 /home/python]#python test.py

This is stdin

Isstdin

 

    Before any lines have been read, filename() returns None and both line

    numbers are zero; nextfile() has no effect.  After all lines have been

    read, filename() and the line number functions return the values

    pertaining to the last line read; nextfile() has no effect.

[ ] 没有行读取前, filename 返回 None 和行号为 0 nextfile 也不起作用。在所有行被读后, filename 和获取行号的函数才能返回到已经读的当前行。 Nextfile 才能起作用。

[ ] 自己测试。

    All files are opened in text mode.  If an I/O error occurs during

    opening or reading a file, the IOError exception is raised.

    [ ] 所有的文件以文本模式打开,如果在打开或者读一个文件时发生了一个 I/O 错误,将会产生一个 IOError 异常。

    If sys.stdin is used more than once, the second and further use will

    return no lines, except perhaps for interactive use, or if it has been

    explicitly reset (e.g. using sys.stdin.seek(0)).

[ ] 如果标准输入用了多次,第二次将不会返回任何行,除在交互模式下,或者将其重置。

[ ]

#!/usr/bin/env python

import fileinput,sys

 

for line in fileinput.input():

    print "line1:",line,

fileinput.close()

sys.stdin.seek(0)

for line in fileinput.input():

    print "line2:",line,

fileinput.close()

 

[root@newpatch3 /home/python]#python test.py

a

b

c

line1: a

line1: b

line1: c

e

f

g

line2: e

line2: f

line2: g

 

    Empty files are opened and immediately closed; the only time their

    presence in the list of filenames is noticeable at all is when the

    last file opened is empty.

   

    It is possible that the last line of a file doesn't end in a newline

    character; otherwise lines are returned including the trailing

    newline.

   

    Class FileInput is the implementation; its methods filename(),

    lineno(), fileline(), isfirstline(), isstdin(), nextfile() and close()

    correspond to the functions in the module.  In addition it has a

    readline() method which returns the next input line, and a

    __getitem__() method which implements the sequence behavior.  The

    sequence must be accessed in strictly sequential order; sequence

    access and readline() cannot be mixed.

    [ ] fileinput 是这个的实例;它的方法有 filename(),…. 对应的功能在这个模块中。除此之外还有 readline 方法,返回下一行输入,和 __getitem__() 方法。该序列中,必须严格按顺序访问;序例访问和 readline 不能混淆。

    Optional in-place filtering: if the keyword argument inplace=1 is

    passed to input() or to the FileInput constructor, the file is moved

    to a backup file and standard output is directed to the input file.

    This makes it possible to write a filter that rewrites its input file

    in place.  If the keyword argument backup=".<some extension>" is also

    given, it specifies the extension for the backup file, and the backup

    file remains around; by default, the extension is ".bak" and it is

    deleted when the output file is closed.  In-place filtering is

    disabled when standard input is read.  XXX The current implementation

does not work for MS-DOS 8+3 filesystems.

[ ] 这段话总的意思是说 ,inplace 如果设为 1 ,那么就将读到的行,输出到输入文件中。如果有 backup 这个参数,就会将源文件内容输入到备份文件中,输出还会输出到输入文件中。

[ ]

(1A)#!/usr/bin/env python

import fileinput,sys

 

for line in fileinput.input("1.txt", inplace=0):

    print line,

[root@newpatch3 /home/python]#python test.py

1a

2a

3a

4a

[root@newpatch3 /home/python]#more 1.txt

1a

2a

3a

4a

(1B)    #!/usr/bin/env python

import fileinput,sys

 

for line in fileinput.input("1.txt",inplace=1):

print “test”,

[root@newpatch3 /home/python]#python test.py

[root@newpatch3 /home/python]#more 1.txt

test test test test

通过 1A 1B 可以发现,我们如果不指定 backup 的话,就会将输出直接写入到 1.txt 文件中。

(2) #!/usr/bin/env python

import fileinput,sys

 

for line in fileinput.input("1.txt",inplace=1,backup=".bak"):

    print "test\n",

[root@newpatch3 /home/python]#ls

1.txt       1.txt.bak     2.txt  test.py

[root@newpatch3 /home/python]#more 1.txt

test

test

test

test

[root@newpatch3 /home/python]#more 1.txt.bak

1a

2a

3a

4a

 

    Performance: this module is unfortunately one of the slower ways of

    processing large numbers of input lines.  Nevertheless, a significant

    speed-up has been obtained by using readlines(bufsize) instead of

    readline().  A new keyword argument, bufsize=N, is present on the

    input() function and the FileInput() class to override the default

buffer size.

[ ] 对与处理大量的输入行的处理是不理想的。然而,一个重要的加速已使用 readlines bufsize )来代替ReadLine ()。一个新的关键参数, bufsize=N, 是在input() 函数中存在的和FileInput() 类中覆盖buffer size 的默认值。总的一句话,通过buffer size 这个关键函数可以提高大量的输入。


本文转自hahazhu0634 51CTO博客,原文链接:http://blog.51cto.com/5ydycm/305488,如需转载请自行联系原作者
相关文章
|
Java Shell 测试技术
Python中fileinput模块使用
fileinput模块可以对一个或多个文件中的内容进行迭代、遍历等操作。该模块的input()函数有点类似文件 readlines()方法,区别在于前者是一个迭代对象,需要用for循环迭代,后者是一次性读取所有行。
896 0
|
Python
【Python标准库:fileinput】优雅的读取文件
每当我写个脚本涉及文件输入时,一般写法都是下面这种 import sys file_in = sys.argv[1] for line in open(file_in, 'r'): commands 这个操作有一个缺点就是,如果我想从管道里面传入数据给Python的话,就会报错,因为原代码要求文件而不是标准输入。
1285 0
|
开发工具 Python 人工智能
|
Python
【Python】模块 fileinput
一 简介   fileinput 是python 提供的一个可以快速遍历,修改一个或者多个文件的模块。我们可以使用该模块进行文本替换 并做文件备份。二 使用2.1 使用方法 fileinput.input (files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None) files   为要读入的文件 或者文件列表 files=1.txt files=['1.txt','2.txt'] inplace 是原地替换与否,为True时表示重写原文件。
1062 0
|
8天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
8天前
|
程序员 开发者 Python
Python网络编程基础(Socket编程) 错误处理和异常处理的最佳实践
【4月更文挑战第11天】在网络编程中,错误处理和异常管理不仅是为了程序的健壮性,也是为了提供清晰的用户反馈以及优雅的故障恢复。在前面的章节中,我们讨论了如何使用`try-except`语句来处理网络错误。现在,我们将深入探讨错误处理和异常处理的最佳实践。
|
12天前
|
缓存 监控 Python
解密Python中的装饰器:优雅而强大的编程利器
Python中的装饰器是一种强大而又优雅的编程工具,它能够在不改变原有代码结构的情况下,为函数或类添加新的功能和行为。本文将深入解析Python装饰器的原理、用法和实际应用,帮助读者更好地理解和利用这一技术,提升代码的可维护性和可扩展性。