代码如下:
import os ## for os.path.isfile()</P><P>def dealline(line) :<BR> print(line) ## 针对line我可以做很多事情</P><P>def getfilename() :<BR> return input('Please input file name(input exit() for exit):').strip()</P><P>class more : ## MORE功能<BR> linenum = 0<BR> size = 10<BR> def work(self) :<BR> if self.linenum >= self.size :<BR> if input('--MORE--').strip().lower() == 'exit()' :<BR> return False<BR> self.linenum = 0<BR> else :<BR> self.linenum += 1<BR> return True</P><P>while True :<BR> try :<BR> filename = getfilename()</P><P> if filename.lower() == 'exit()' : ## 退出<BR> break</P><P> if os.path.isfile(filename) : ## 判断文件是否存在</P><P> f = open(filename)<BR> try :<BR> <strong style="color:transparent">来2源gaodaima#com搞(代@码&网</strong> lines = f.readlines()</P><P> m = more()<BR> for line in lines:<BR> if False == m.work() :<BR> break<BR> dealline(line)<br><br> ## input()<BR> finally :<BR> f.close()</P><P> else :<BR> print('File does not exists.')<BR> ##input()<BR> except :<BR> print('Input Error!')<BR>
还可以用
with open(filename) as fh: <BR> for line in fh: <BR> yield line <BR>
输出每一行