user3746295
user3746295

Reputation: 101

text mining: read text files in Python

When I use R, I can read many text documents which are contained in a file folder for one time. However, I just started to learn Python. When I use command: file = open('c:/txt/Romney', 'r'),trying to open all text files contained in that Romney folder, I found out I have to read inside text files one by one, I cannot read all for one time just like I do in R. Any suggestion?

Upvotes: 0

Views: 1848

Answers (2)

Patrick Collins
Patrick Collins

Reputation: 10594

In addition to Dave Yarwood's answer, if what you actually wanted to do was concatenate the files, you could do it with:

from os import listdir
from os.path import isfile, join
from itertools import chain

path = "C:/txt/Romney"
files = [open(f) for f in listdir(path) if isfile(join(path,f))]

for line in chain(*files):
    do_something_with(line)

(just for fun, because I've never used itertools.chain to string together files before)

Upvotes: 2

Dave Yarwood
Dave Yarwood

Reputation: 3010

In a language like Python, you'll need to use a for loop to read the contents of each file, one at a time.

(Related: How to list all files of a directory in Python)

from os import listdir
from os.path import isfile, join

path = "C:/txt/Romney"
files = [ f for f in listdir(path) if isfile(join(path,f)) ]

for file in files:
    with open file as f:
        text = f.read()
        do_something_with(text)

Upvotes: 5

Related Questions