Copy codeThe Code is as follows:
Import OS # for OS. path. isfile ()
Def dealline (line ):
Print (line) # I can do many things for line
Def getfilename ():
Return input ('Please input file name (input exit () for exit): '). strip ()
Class more: # MORE functions
Linenum = 0
Size = 10
Def work (self ):
If self. linenum> = self. size:
If input ('-- MORE --'). strip (). lower () = 'exit ()':
Return False
Self. linenum = 0
Else:
Self. linenum + = 1
Return True
While True:
Try:
Filename = getfilename ()
If filename. lower () = 'exit () ': # exit
Break
If OS. path. isfile (filename): # determine whether the file exists
F = open (filename)
Try:
Lines = f. readlines ()
M = more ()
For line in lines:
If False = m. work ():
Break
Dealline (line)
# Input ()
Finally:
F. close ()
Else:
Print ('file does not exists .')
# Input ()
Except t:
Print ('input Error! ')
You can also use
Copy codeThe Code is as follows:
With open (filename) as fh:
For line in fh:
Yield line
Output each line