How do you read Top 10 lines in Python?

How do you read Top 10 lines in Python?
Answer
Use file.
readline() to print the first n lines of a file
  1. a_file = open(“file_name.txt”) Open “file_name.txt”
  2. number_of_lines = 3.
  3. for i in range(number_of_lines): Print the first number_of_lines lines of a_file.
  4. line = a_file. readline()
  5. print(line)
 

Tìm kiếm

Back
Top