# Is it possible to create a while loop that determines when the end of a file has been reached?

1 vote
268 views

## Problem:

Hello all,

I am a new, college student who is learning python programming. I am thinking to write a program that will input a text file and read all the vowels from it and determines when it comes to the end of the file. So, is it possible to create a while loop that determines when the end of a file has been reached?

## Solution:

The answer is: YES! You can write a code that read vowels from the input text and determines the end of the line.

``````vowels = {'A','E','I','O','U','a','e','i','o','u'}

with open('my_file.txt') as f:
vow = sum(ch in vowels for line in f for ch in line.strip())
while True:
if line == '':
break``````

Here, we defined our vowels and checked it into our input, and then checked through a While-loop if it reaches the end of the line or not!

Hence, (‘ ‘) define an empty string or (EOL) and of the line.

Thanks.

## Related questions

1 vote
918 views
Problem: Hello Pal, I have a query regarding the python programming language. The question is, which method will return an empty string when it has attempted to read beyond the end of a file? Do you have any idea about this? Thanks
43 views
Problem: Hi there! I am confused in while loops and I need to write a program in which a value is taken as input from the user. If the value is less than 0, the program should increment it by 5 as long as the value remains less than 0. As the value becomes greater than or equal to 0, it should print the number of times the value has been incremented and the final value. Any help??
1 vote
10 views
Problem: How to break a while loop with if condition ?
1 view
Problem: What is the difference between an if statement and a while loop?
2 views
Problem: I have written following code that prints the table of numbers from 1 to 10. ch = 'Y' while ch == 'Y': for i in range(1,10): for j in range(1,10): print(i, "x", j, "=", i*j) print("Tables printed: ",i) When I run this program, it ... exit the while loop and print the number of tables printed on the screen. I do not know what I am doing wrong and why my program keeps running. Please help!
9 views
Problem: How Inner while loop prints a single row after its complete execution? Outer while loop helps to print n number of rows. In other words, outer while loop prints the rows, while inner while loop prints columns in each row.
20 views
Problem: Hi there! I want my while loop to stop after it reaches a certain point and jump out of the loop. Is there any mechanism that I can use? Please help!