• 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

Please log in or register to answer this question.

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 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 675 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, 2020 Gavin 15.3k points
1 vote
1 answer 94 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
2 answers 761 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
0 votes
1 answer 1.6K 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
0 votes
1 answer 14 views
it will be of great help ,if you define what is __repr__ in python...
asked Nov 3, 2020 user1 160 points
0 votes
0 answers 30 views
I saw this code to find difference between diagonal sums of a square matrix. #!/bin/python3 import math import os import random import re import sys def diagonalDifference(arr): d1=0 d2=0 a=n-1 print(arr[1][1]) for i in range(0,n): d1+=arr[i][i] ... ['OUTPUT_PATH'], 'w') with fptr = sys.stdout, the output shows no difference. What then is the difference between functions of these two statements?
asked Oct 29, 2020 psandprop 2.4k points
0 votes
1 answer 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 501 views
Probem : I am getting bellow error new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
asked Nov 7, 2019 peterlaw 6.9k points
0 votes
2 answers 107 views