Help needed on readline() in Python 3.0 | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Help needed on readline() in Python 3.0

Consider the following code: # Line by Line Printer fh = open('mbox.txt') fl = fh.readline(10) for line in fl: print(line) I intend to print the first 10 lines of the txt file, but the output is only the first line. What's the problem? Should I switch to readlines() ?

28th Dec 2016, 8:32 AM
Ethan ZHOU
Ethan ZHOU - avatar
1 Réponse
0
with open("Filename",'r') as f: for i in range(10): print(f.readline())
28th Dec 2016, 9:38 AM
GuoKet
GuoKet - avatar