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

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution:

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

file=open()

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:

file=open('IH.txt','r')
text=""
for i in range(6):
    text += file.readLine()
print(text)
file.close()

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

0 votes
1 answer 1 view
1 view
Problem: I've just created a new table in my sqlserver name exportable now I'm trying to push out using cmd bcp but om getting the following error: SQLState = S1000, NativeError = 0 Error = [Microsoft][ODBC Driver 13 for SQL Server]Unable to open BCP host data-file
asked 5 days ago ummesalma 25.2k points
1 vote
1 answer 268 views
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? Thanks in advance.
asked Mar 16, 2020 Gavin 15.3k points
1 vote
1 answer 918 views
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
asked Mar 16, 2020 Gavin 15.3k points
0 votes
1 answer 2 views
2 views
Problem: How can I read c++ words from file?
asked 2 days ago muktaa 4.9k points
0 votes
1 answer 1 view
1 view
Problem How to read data from a specified file in c++
asked Mar 31 Subhendu 4.4k points
0 votes
1 answer 3 views
3 views
Problem: How do we read data from a file in C++? Please answer.
asked Mar 26 Code Learner 9.2k points
1 vote
2 answers 1.1K views
1.1K 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
1 vote
1 answer 186 views
186 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 24 views
24 views
Problem: I get an error when I try to open a file in Python. Here is my code : >>> import os.path >>> os.path.isfile('/path/to/file/t1.txt') >>> True >>> myfile = open('/path/to/file/t1.txt','w') >>> myfile >>> <open file '/path/to/file/ ... <module> IOError: File not open for reading I also tried: for line in myfile: print(line) and I got the same error. Does anybody know why this error occurs?
asked Feb 19 Mashhoodch 13k points
0 votes
1 answer 2 views
2 views
Problem: Any ideas on how to read explain plan in oracle with example?
asked Apr 5 ashik 12.9k points