Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Working around multiple files in a folder

39 views
Skip to first unread message

subhaba...@gmail.com

unread,
Nov 21, 2016, 2:27:57 PM11/21/16
to
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.


Emile van Sebille

unread,
Nov 21, 2016, 4:20:27 PM11/21/16
to
at this point, there's nothing left to be read from FI having been fully
drained to populate FI1 -- maybe you want to loop over FI1 instead?

Emile

Veek M

unread,
Dec 8, 2016, 11:13:27 PM12/8/16
to
*goggles in shock* that was painful to read! The best I could make of it
was, he's trying to match words in some.txt against a dictionary.

(OP) should not code like a horror movie. Try to avoid dismembering your
variable names and numbering the body parts like a serial killer.
Instead try to pick names that matter. Use functions to hide the gory
complexity..

Maybe like this (I dunno what you are doing)..

dict_file = '/python27/Dictionaryfile'
txt_file_path = '/Python27/*.txt'


def open_file(fname, mode='r'):
lines = open(fname, mode).read()
words = lines.lower().split()
return words

def get_files(path):
file_list = []
for fname in glob.glob(path):
file_list.append(fname)

return file_list


word_list = open_file(dict_file, mode)
file_list = get_files(txt_file_path)


for fname in file_list:
do something and stick this in a func when you know what you were doing

Otherwise you'll get minimal help online because your program is
unreadable.. in-spite of the frivolous comments.
0 new messages