I'm writing a program where the user inputs a file name and then the file and prints only the first 2 and the last 2 lines of the file. I have worked out how to print the first two lines and I have tried to print the last two lines but have hit a hiccup. Is anyone able to explain what I did wrong?
f1 = open(input("Source file name: "))
line1= f1.readline()
line2= f1.readline()
line12= f1.readline()
line13= f1.readline()
print("Output:",line1,line2,line12[-1],line13[-2], sep= "")
f1.close()
The file is 13 lines long so the output should be as follows:
output:
line 1
line 2
line 12
line 13