How to read n number of files in n variables and then add those variables in a list?

Viewed 72

This is my code:

file_input1 = open('Amazon_Indi_Seller.py', 'r')
f1 = file_input1.read().lower()

file_input2 = open('Amazon_Prices.py', 'r')
f2 = file_input2.read().lower()

documents = [f1, f2]
import nltk, string, numpy

stemmer = nltk.stem.porter.PorterStemmer()

lemmer = nltk.stem.WordNetLemmatizer()
def LemTokens(tokens):
    return [lemmer.lemmatize(token) for token in tokens]
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
def LemNormalize(text):
    return 
LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))

from sklearn.feature_extraction.text import CountVectorizer
LemVectorizer = CountVectorizer(tokenizer=LemNormalize, 
stop_words='english')
LemVectorizer.fit_transform(documents)

Instead of reading 2 files i want to read all the files in a directory. And read them individually so that later I can add those variables in a list named documents.

4 Answers

You could collect all the in a list, for example: lst = []

 for file in os.listdir():
     file_input = open(file,"r")
     lst.append(file_input.read())

One extra recommendation - in general it might be wise to store the contents of a file as a collection of its lines by for example using file_input.readlines() which returns a list of lines.

Create a list of all filenames and then iterate over filename list and add their content in a dictionary.

from collections import defaultdict    #imported default dictionary

result = defaultdict()    #created empty default dictionary
filenames = ['name1.py', 'name2.py', 'name3.py']   #added filenames to a list

for name in filenames:   #iterate over filename list
    with open(name, 'r') as stream:   #open each file
        data = stream.readlines()     #read contents lines by line (readlines return list of lines)
        result[name] = data    # set name as key and content as value in dictionary

print(result)   

In this way you will have a dictionary with keys as filenames and values as their contents

You can use the code mentioned below,

  import os

    def read_files(file):
        file_input1 = open(file, 'r')
        f1 = file_input1.read()
        return f1

    files = ['sample.py', 'Amazon_Indi_Seller.py']
    data = list()
    for file in files:
        data.append(read_files(file))
    print(data)

The above code will used to read the files mentioned in the list

import os

def read_files(file):
    file_input1 = open(file, 'r')
    f1 = file_input1.read()
    return f1

src = r'DIRECTORY PATH'
data = list()
for file in os.listdir(src):
    data.append(read_files(file))

print(data)

And the above code will read all the files from the directory mentioned

If the directory may include other directories with files, which you want to read to - use os.walk

Here is sample code from the official documentation:

import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
    print root, "consumes",
    print sum(getsize(join(root, name)) for name in files),
    print "bytes in", len(files), "non-directory files"
    if 'CVS' in dirs:
        dirs.remove('CVS')  # don't visit CVS directories
Related