简体   繁体   中英

Python: How do I output all the text contents of a directory containing .txt files as a list?

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:)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM