python标准库fileinput模块学习

# encoding:utf-8

######################
# fileinput模块
# fileinput模块可以让你能够轻松地遍历文本文件所有的行
######################
import fileinput
import os

#创建文件,并添加内容
file = open("test.txt","w")
for i in range(97,123):
	file.write(chr(i)+os.linesep)
file.close()

#为test.txt添加行号
for line in fileinput.input("test.txt"):
	line=line.rstrip()
	num=fileinput.lineno()
	print '%-40s #%2i' % (line,num)
print "file count line:%i" % fileinput.filelineno()

# print fileinput.__all__
# print help(fileinput.filename)?



   

你可能感兴趣的:(python标准库fileinput模块学习)