• Register
1 vote
990 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

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution:

I understand that you wanted to read a file as input by your program. If I am not wrong you asked the question, which method will return an empty string when it reached the end of the file.

When a program reaches the (EOL) end of the line the .read() method returns an empty string whether the input file has a string or not.

f = open('test_file.txt')

>>> f.read() # reading the entire file
'Test File has data.'

>>> f.read() # Here you've reached the end of the file 
''

f.read() function return (‘ ‘). Here the program reached the end of the file.

I hope this will help you. To understand the .read() function better, follow the LINK

13 9 6
94,260 points

Related questions

1 vote
1 answer 282 views
282 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? Thanks in advance.
asked Mar 16, 2020 Gavin 15.3k points
0 votes
1 answer 16 views
16 views
Problem: I need a good explanation (references are a plus) on Python's slice notation.To me, this notation needs a bit of picking up. It looks extremely powerful, but I haven't quite got my head around it.
asked Apr 24 sumaiya simi 43.9k points
1 vote
1 answer 206 views
206 views
Problem: I wrote a program that asks the user to enter the amount of a purchase and then it sums up the total bill with 15% vat and 0.07% service tax. I wrote the program in python: vat = 0.15 sales_tax = 0.075 product_price = float(input("Please enter the ... not defined I can not solve this thing. I am new in python programming language so please help me to solve this error. Thanks in advance.
asked May 6, 2020 samhaz 5k points
1 vote
1 answer 54 views
54 views
Problem: Is there any simple way to read the whole ASCII file into std::string in C++:? Thanks
asked Mar 9, 2020 Gavin 15.3k points
1 vote
1 answer 137 views
137 views
Problem: Hello people I am doing python programming for a few days. I am still learning. Could anybody please help me by showing how can I import a text file and read five lines from it by my python program? If possible make it as easy words as possible for me. Thanks.
asked Mar 11, 2020 Gavin 15.3k points
0 votes
1 answer 50 views
50 views
Problem: The following C++ code uses a ifstream object to read integers from a text file (which has one number per line) until it hits EOF. Why does it read the integer on the last line twice? How to fix this? Code: #include <iostream> #include <fstream> using namespace std; int main() { ... txt has integers, one per line while (!iFile.eof()) { int x; iFile >> x; cerr << x << endl; } return 0; }
asked Dec 25, 2020 Mashhoodch 13k points
0 votes
1 answer 19 views
0 votes
1 answer 102 views
102 views
Problem: Hi there! I am new to the world of programming. My programming teacher gave me an assignment in which I have to write a program in which there would be a function that will take two strings as input from user and will compare the size of both strings ... the larger string of the two. I have no clue how to do that. Please provide me with the sample program with proper description. Thanks
asked Nov 25, 2020 Code Learner 9.8k points
0 votes
1 answer 1.3K views
1.3K views
The code below is based on code.activestate.com/recipes/413614/ However, the key point of the recipe - that it provides a way to break out of the iteration on an iterator if the iterator is empty - doesn't seem to work here, instead behaving ... next_choice = choice_iterator.next() except StopIteration: print "No matches. Exiting..." sys.exit() else: choice_iterator = itertools.chain([next_choi
asked Oct 28, 2020 psandprop 2.4k points
0 votes
1 answer 6 views
6 views
Problem: Please assist me in resolving this issue: which method will return the element that has the id attribute with the specified value
asked Apr 1 rakib1 51.5k points