Hello so i am trying to read lines from a txt file. My code is the following:
import sys
accFiletypes = '.txt'
f = None
filnavn = None
correctFiletype = False
while (correctFiletype == False):
filnavn = input("Filename (Type exit to leave):")
if (filnavn.endswith(accFiletypes) == True):
try:
f = open(filnavn, 'r')
correctFiletype = True
print("File successfully opened!")
except IOError:
print("File is not existing")
elif (filnavn == "exit"):
sys.exit("Program closed")
else:
print("Accepted filetypes: " + accFiletypes)
line = f.readline
print(line())
print(line(2))
print(line(3))
print(line(4))
print(line(5))
print(line(6))
f.close()
This prints the following:
Filename (Type exit to leave):test.txt
File successfully opened!
0000 00000000
00
00
0000
1
0000 0
The first 10 lines in "test.txt"
0000 00000000
0000 00001
0000 00001111
0000 000099
0000 00009999
0000 0000w
0000 5927499
0000 634252
0000 6911703
0000 701068
I want it to print out the lines in the txt file but i prints something completely different. What do i do?