readlines()
is a method, call it:
list1 = text_file.readlines()
Also, instead of loading the whole file into a python list, iterate over the file object line by line. And use with context manager:
with open("/Users/abc/test.txt", "r") as f:
list2 = []
for item in f:
number = 0
while number < 5:
list2.append(item + str(number))
number += 1
print list2
Also note that you don't need to call str()
on item
and you can use +=
for incrementing the number
.
Also, you can simplify the code even more and use a list comprehension with nested loops:
with open("/Users/abc/test.txt", "r") as f:
print [item.strip() + str(number)
for item in f
for number in xrange(5)]
Hope that helps.