Load stop words from multiple text files in a directory

Question:

I have a directory with different text files of stop words. I want to import them all together, using with open(), but am getting an error:

file_list = glob.glob(os.path.join(os.getcwd(), "Directory", "*.txt"))

corpus = []

for file_path in file_list:

    with open(file_path,'r') as stop_words:
        stop_words.append(stop_words.read())
stopWords = stop_words.read().lower()
stopWordList = stopWords.split('n')
stopWordList[-1:] = []    

AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘append’

Thanks

Asked By: iFrankenstein

||

Answers:

Here is one way to get all words into a list.

file_list = glob.glob(os.path.join(os.getcwd(), "Directory", "*.txt"))

stopwords = []

for file_path in file_list:
    with open(file_path, 'r') as f:
        stopwords.extend(f.read().lower().splitlines())
Answered By: fsimonjetz
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.