• Register
1 vote


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.

12 7 7
15,250 points

1 Answer

0 votes


You can import a text file by calling a simple function in python named 


Let’s assume we have a text file IH.txt and we want to import it and read it in our program. Thus I write:

for i in range(6):
    text += file.readLine()

The read() function reads the whole file and readLine() function reads only one line from the text file.

The loop has 5 iterations and in each iteration, the next line from the file is appended to text via concatenation.

13 9 6
94,240 points

Related questions

1 vote
1 answer 185 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 Gavin 15.3k points
1 vote
1 answer 318 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
asked Mar 16 Gavin 15.3k points
1 vote
1 answer 47 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 samhaz 5k points
0 votes
1 answer 1.3K views
Problem : I am very new to Quartz. So whenever I try to install Quartz on my system using pip install Quartz, I face the following error. Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 1, in <module> ... [Errno 2] No such file or directory: 'requirements.txt' I am using Mac OSX with Anaconda. Does anyone have any clue on this issue?
asked Nov 27, 2019 alecxe 7.5k points
1 vote
2 answers 471 views
Problem : I am getting following error while trying to read the CSV file with R. in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : eof within quoted string
asked Nov 11, 2019 peterlaw 6.9k points