My question: Program to read first n lines from a txt file,get n as user input. I tried this,
f1=open('sampletest.txt','r')
n=int(input("Enter number of lines to be read from the txt file:"))
line=f1.readlines(n)
print(line)
f1.close()
My txtfile contents which I named sampletest are as follows:
Tom and Jerry are cartoon characters.
Tom is a cat.
Jerry is a mouse.
They have a common friend,Spike.
Spike is a dog.
The output I get by executing my python code: Enter number of lines to be read from the txt file:3 ['Tom and Jerry are cartoon characters.\n']
How can I correct my code?I am new to coding. Please help. Thanks in advance.