Python Forum
My Loop for printing text file goes to infinity
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My Loop for printing text file goes to infinity
#1
I have to print a text file (cities) in a While Loop but it goes to infinity
 
filehandle=open("cities.txt", "r")
f = filehandle.readline()
while f != "":
    print(f)
filehandle.close()
Reply
#2
while f != "":
That says that if f isn't an empty string, keep printing. So if cities.txt has anything in it, you print forever. And I think only print the first line again and again.

What does the assignment say exactly?
Reply
#3
The assignment is to make a menu and the first option of that menu is to print the list of cities.
So how would i use the loop to print the text file using .readline(). How would it print forever? If f has come to a line which is empty wouldnt it stop?
Reply
#4
f = open('cities.txt', r)
while True:
    line = f.readline()
    print(line)
    if not line:
        break
f.close()
Your readline() needs to be inside the loop or it never changes. If it never changes then it can never be an empty line.
Reply
#5
(Feb-08-2020, 04:43 AM)Nomex Wrote: How would it print forever? If f has come to a line which is empty wouldnt it stop?
No it would not stop,it always read whole file to the end using while or for loop.
A better way,so can iterate over file object line bye line(do not need readline()).
with open('cities.txt') as f:
    for line in f:
        print(line.strip())
Example how stop(break out of loop) if there is a empty line.
with open('cities.txt') as f:
    for line in f:
        if line in ('', '\n'):
            break
        print(line.strip())
Reply
#6
(Feb-08-2020, 09:21 PM)snippsat Wrote: A better way,so can iterate over file object line bye line(do not need readline()).

I think the assignment specifies readline().
Reply
#7
The problem of infinity was there because i did not write f=f.readline() in the loop again. After that it worked perfectly. Thanks by the way!
Reply
#8
(Feb-10-2020, 01:42 PM)Nomex Wrote: The problem of infinity was there because i did not write f=f.readline() in the loop again. After that it worked perfectly. Thanks by the way!

Although it works, it's better to look at @snippsat example, because it's much better and pythonic than what you do
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing During a Loop apeltes 16 5,033 Oct-21-2021, 12:19 AM
Last Post: apeltes
  Help with for-loop printing. I want it to print only once. blacklight 2 6,860 Jun-26-2020, 02:23 AM
Last Post: pyzyx3qwerty
  Convert text from an image to a text file Evil_Patrick 5 4,214 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  reading text file and writing to an output file precedded by line numbers kannan 7 10,241 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Newly written .txt file not printing Crackity 11 5,927 Oct-20-2017, 06:43 AM
Last Post: gruntfutuk
  Command line inputs not printing to Log File ijosefson 1 3,310 Oct-19-2017, 06:41 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020