Rvs
Rvs

Reputation: 53

reading all the text files from directory

I am new to python and I am using following code to pull output as sentiment analysis:

import json
from watson_developer_cloud import ToneAnalyzerV3Beta
import urllib.request
import codecs
import csv
import os
import re
import sys
import collections
import glob
ipath = 'C:/TEMP/' # input folder
opath = 'C:/TEMP/matrix/' # output folder
reader = codecs.getreader("utf-8")
tone_analyzer = ToneAnalyzerV3Beta(
    url='https://gateway.watsonplatform.net/tone-analyzer/api',
    username='ABCID',
    password='ABCPASS',
    version='2016-02-11')
path = 'C:/TEMP/*.txt'   
file = glob.glob(path)
text = file.read()
data=tone_analyzer.tone(text='text')
for cat in data['document_tone']['tone_categories']:
    print('Category:', cat['category_name'])
    for tone in cat['tones']:
        print('-', tone['tone_name'],tone['score'])
        #create file

In the above code all I am trying to do is to read the file and do sentiment analysis all the text file stored in C:/TEMP folder but I keep getting and error :'list' object has no attribute 'read'

Not sure where I am going wrong and I would really appreciate any help with this one. Also, is there a way i can write the output to a CSV file so if I am reading the file

ABC.txt and I create a output CSV file called ABC.csv with output values.

Thank You

Upvotes: 1

Views: 15755

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

glob returns a list of files, you need to iterate over the list, open each file and then call .read on the file object:

files = glob.glob(path)
# iterate over the list getting each file 
for fle in files:
   # open the file and then call .read() to get the text 
   with open(fle) as f:
      text = f.read()

Not sure what it is exactly you want to write but the csv lib will do it:

from csv import writer

files = glob.glob(path)
# iterate over the list getting each file
for fle in files:
   # open the file and then call .read() to get the text
   with open(fle) as f, open("{}.csv".format(fle.rsplit(".", 1)[1]),"w") as out:
       text = f.read()
       wr = writer(out) 
       data = tone_analyzer.tone(text='text')
       wr.writerow(["some", "column" ,"names"]) # write the col names

Then call writerow passing a list of whatever you want to write for each row.

Upvotes: 8

Related Questions