Solution:
You are employing str
systems on an open file object.
You can read the file as a list of lines by easily calling list()
on the file object:
with open('goodlines.txt') as f:
mylist = list(f)
This does add the newline characters. You can slice those in a list contact:
with open('goodlines.txt') as f:
mylist = [line.rstrip('\n') for line in f]
Attempt this:
>>> f = open('goodlines.txt')
>>> mylist = f.readlines()
open()
function returns a file object. And for file object, there is no system like splitlines()
or split()
. You could use dir(f)
to view all the methods of file object.
You're not reading the file content:
my_file_contents = f.read()
Without calling read()
or readlines()
loop over your file object:
f = open('goodlines.txt')
for line in f:
print(line)
In case you want a list out of it (without \n
as you asked)
my_list = [line.rstrip('\n') for line in f]