# define a function to get .txt files in a folder
from os import listdir
def list_textfiles(directory):
"Return a list of filenames ending in '.txt' in DIRECTORY."
textfiles = []
for filename in listdir(directory):
if filename.endswith(".txt"):
textfiles.append(directory + "/" + filename)
return textfiles
# define a function to read the text in a .txt file
def read_txt(filename):
try:
f = open(filename,'r')
text = f.read()
finally:
if f:
f.close()
return text
#import harry potter textfiles
filenames = list_textfiles('HP')
corpus = []
for f in filenames:
corpus.append(read_txt(f))
print len(corpus[0])
448822