Reputation: 1
I have a directory file containing three thousand .txt files which contain scraped text paragraphs.
I'm trying to output a list in python in which each list row contains the contents of one of the .txt files
I'm very new to python and all I've managed to get to so far is;
import glob
mylist = [f for f in glob.glob("/Users/Downloads/Datasets/transcripts/*.txt")]
However, all I get from this is a list of 3,000 with each row containing the title of the .txt file and not the contents
Was wondering if anyone could help out, thanks :)
Upvotes: 0
Views: 60
Reputation: 1824
The glob
method returns a list of file names (or paths). You need iterate over that list, opening the files and appending the contents to your list as you go:
texts = []
for text_file in glob.glob("/Users/Downloads/Datasets/transcripts/*.txt"):
with open(text_file, 'r') as f:
t = f.read()
texts.append(t)
Upvotes: 1