Reputation: 1
With this code:
def createWordList(filename):
file=open(filename,'r')
s=file.read()
file.close()
L=s.split()
return L
And this text file: http://www.cs.ucsb.edu/~buoni/cs8/labs/lab05/wordlist.txt
I am to return a list of all the words in the text file. But when the function is called a la:
createWordList('wordlist.txt')
My computer (core-i5) takes about 5-10 minutes to perform the task and then ultimately freezes. It can return the string of individual words in about 2 seconds though.
Upvotes: 0
Views: 118
Reputation: 2565
f=open('wordlist.txt','r+')
listofwords=[]
for line in f:
listofwords.append(line)
print(listofwords)
I faced no problem. took exactly 0.049 sec to form the list(process program without print). printing will take lot of time.
Upvotes: 1
Reputation: 2473
The 'file' object is a iterable, and you can iterate it with a 'for' stament. This is more memory efficient. Read this: Methods of File Objects
So, try this code:
def createWordList(filename):
myList = []
input_file = open(filename, mode='r')
for line in input_file:
myList.append(line)
return myList
How works:
It takes no more than 3 seconds in process the whole file.
Upvotes: 0