how to read unknown no. of lines in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to read unknown no. of lines in python

29th Aug 2018, 11:55 AM
Chandra Sekhar Satyavarapu
Chandra Sekhar Satyavarapu - avatar
5 Answers
+ 1
Code : file = open("filename.txt", "r") for line in file: print(line) file.close() Output : >>> Line 1 text Line 2 text Line 3 text >>>
29th Aug 2018, 12:35 PM
Sam Pache
Sam Pache - avatar
+ 3
Not sure if this is what you need, but it will read multipline lines of input from the console and only stop if an empty line is entered (probably doesn't work in SoloLearn, but it works in a console). Everything will be stored in the variable c. c = '' while True: ui = input('>>> ').strip() if ui != '': c += ui + '\n' else: # remove last exit character c = c[:-1] break print(c)
29th Aug 2018, 2:51 PM
Anna
Anna - avatar
0
not sure what you mean but random module might be one way
29th Aug 2018, 12:26 PM
Markus Kaleton
Markus Kaleton - avatar
0
Sam Pache unknown, but yeah basic principle
29th Aug 2018, 12:38 PM
Markus Kaleton
Markus Kaleton - avatar
0
not from file how to read directly from console
29th Aug 2018, 1:21 PM
Chandra Sekhar Satyavarapu
Chandra Sekhar Satyavarapu - avatar