• Register
1 vote
674 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,240 points

Related questions

1 vote
1 answer 240 views
240 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
1 vote
1 answer 93 views
93 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
0 votes
1 answer 71 views
71 views
Problem: Which of the following will open a file named MyFile.txt and allow you to read data from it? a. File file = new File("MyFile.txt"); b. Scanner inputFile = new Scanner("MyFile.txt"); c. File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file); d. PrintWriter inputFile = new PrintWriter("MyFile.txt"); Does any one know correct file name?
asked Feb 22, 2020 maddi86 5.4k points
0 votes
1 answer 431 views
431 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 25 views
25 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 5.3k points
0 votes
1 answer 17 views
17 views
Problem: Hi there! I am newbie and I am want to strip away specific characters from the end of a string. I am programming in C#. I have tried Remove() method as follows: using System; namespace ConsoleApp2 { class Program { static void Main(string[] args) { ... the index of string myself. I want to know that is there any way in C# to remove characters from strings without specifying the index?
asked Nov 27, 2020 Code Learner 5.3k points
0 votes
1 answer 11 views
11 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 5.6k points
0 votes
1 answer 11 views
1 vote
1 answer 76 views
76 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 90 views
90 views
Solutions: I am new in programming and my logic is not good, can anyone give the right solution? Write an expression that evaluates to true if and only if the C-string s equals the C-string "end"?
asked Feb 14, 2020 maddi86 5.4k points