Python3 implements the method of reading the specified row from the file,
This example describes how Python3 can read a specified row from a file. Share it with you for your reference. The specific implementation method is as follows:
# The linecache module of the Python standard library is very suitable for this task import linecachethe_line = linecache. getline ('d:/FreakOut. cpp ', 222) print (the_line) # linecache reads and caches all the text in the file. # If the file is large and only one row is read, the efficiency is low. # The cycle can be displayed. Note that enumerate starts counting from 0, while line_number starts from 1 def getline (the_file_path, line_number): if line_number <1: return ''for cur_line_number, line in enumerate (open (the_file_path, 'ru '): if cur_line_number = line_number-1: return line return ''the _ line = linecache. getline ('d:/FreakOut. cpp ', 222) print (the_line)
I hope this article will help you with Python programming.