Working around multiple files in a folder

subhabangalore at gmail.com subhabangalore at gmail.com
Mon Nov 21 14:27:31 EST 2016


I have a python script where I am trying to read from a list of files in a folder and trying to process something. 
As I try to take out the output I am presently appending to a list.

But I am trying to write the result of individual files in individual list or files.

The script is as follows:

import glob
def speed_try():
    #OPENING THE DICTIONARY
    a4=open("/python27/Dictionaryfile","r").read()
    #CONVERTING DICTIONARY INTO WORDS
    a5=a4.lower().split()
    list1=[]
    for filename in glob.glob('/Python27/*.txt'):
        a1=open(filename,"r").read()
        a2=a1.lower()
        a3=a2.split()
        for word in a3:
            if word in a5:
                a6=a5.index(word)
                a7=a6+1
                a8=a5[a7]
                a9=word+"/"+a8
                list1.append(a9)
            elif word not in a5:
                list1.append(word)
            else:
                print "None"

    x1=list1
    x2=" ".join(x1)
    print x2

Till now, I have tried to experiment over the following solutions:

a) def speed_try():
      #OPENING THE DICTIONARY
      a4=open("/python27/Dictionaryfile","r").read()
      #CONVERTING DICTIONARY INTO WORDS
      a5=a4.lower().split()
      list1=[]
      for filename in glob.glob('/Python27/*.txt'):
         a1=open(filename,"r").read()
         a2=a1.lower()
         a3=a2.split()
          list1.append(a3)
        

    x1=list1
    print x1

Looks very close but I am unable to fit the if...elif...else part. 

b) import glob
def multi_filehandle():
    list_of_files = glob.glob('/Python27/*.txt')
    for file_name in list_of_files:
        FI = open(file_name, 'r')
        FI1=FI.read().split()
        FO = open(file_name.replace('txt', 'out'), 'w') 
        for line in FI:
            FO.write(line)

        FI.close()
        FO.close()

I could write output but failing to do processing of the files between opening and writing.

I am trying to get examples from fileinput.

If anyone of the learned members may kindly suggest how may I proceed.

I am using Python2.x on MS-Windows. 

The practices are scripts and not formal codes so I have not followed style guides.

Apology for any indentation error.

Thanking in advance.





More information about the Python-list mailing list