fileinput模块

fileinput.input (files=Noneinplace=Falsebackup=''bufsize=0mode='r'openhook=None)

参数

files :文件的路径列表

inplace:将标准输出(print方法)的结果写回文件

backup : 备份文件的扩展名,只指定扩展名,如.bak。如果该文件的备份文件已存在,则会自动覆盖。

bufsize :缓冲区大小

mode :读写模式

参数inplace如果设为1,那么就将读到的行,输出到输入文件中。如果有backup这个参数,就会将源文件内容输入到备份文件中,输出还会输出到输入文件中。

#coding:utf-8
'''
1. fileinput.isfirstline() 判断是否是新文件的开始
2. fileinput.filename() 输出文件名称
3. fileinput.filelineno() 输出当前行在所在文件中的行数
4. fileinput.lineno() 输出当前已经输出的总行数
5. fileinput.nextfile() 跳出当前遍历的文件,跳至下个文件
'''
import fileinput
def main():
    for line in fileinput.input([r"G:\\rr.txt",r"G:\\rr2.txt"]):
        #判断是否是新文件的开始,如果是,则输出文件名称
        if fileinput.isfirstline():
            print '\n'+'*'*10
            print fileinput.filename()
        #输出行号、行内容
        print fileinput.filelineno(),line,
        #跳至下个文件
        if line.strip() =="lll":
            fileinput.nextfile()
        #输出当前已经输出的总行数
        print fileinput.lineno(),line,


if __name__ == '__main__':
    main()


[例]

(1A)#!/usr/bin/envpython
importfileinput,sys
 
for line infileinput.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 infileinput.input("1.txt",inplace=1):
print “test”,

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

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

test test testtest

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


操作实例:

==================================

G盘有test.txt文件,内容如下:

I am a test file
want to sing a song for you
do you want to listen?
listen.....
listen.....
listen.....
I heard, that your settled down.
That you, found a girl and your married now.
I heard that your dreams came true.
Guess she gave you things, I didn't give to you.
Old friend, why are you so shy?
I hate to turn up out of the blue uninvited.

--------------------------------------------------------------

程序代码如下:

import fileinput

for line in fileinput.input(r'G:\\test.txt',inplace = True,backup ='.bak'):
    print '%-60s # %2i' %(line, fileinput.lineno())

--------------------------------------------------------------

执行程序后,text.txt内容如下:

I am a test file                                             #  1
want to sing a song for you                                  #  2
do you want to listen?                                       #  3
listen.....                                                  #  4
listen.....                                                  #  5
listen.....                                                  #  6
I heard, that your settled down.                             #  7
That you, found a girl and your married now.                 #  8
I heard that your dreams came true.                          #  9
Guess she gave you things, I didn't give to you.             # 10
Old friend, why are you so shy?                              # 11
I hate to turn up out of the blue uninvited.                 # 12

--------------------------------------------------------------------

且在G盘下生产了test.txt.bak文件,内容与执行程序前的test.txt内容一致。








  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值