Home > OS >  Load stop words from multiple text files in a directory
Load stop words from multiple text files in a directory

Time:08-08

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

CodePudding user response:

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())
  • Related