Ceem Ceem - 5 months ago 8
Python Question

How to read a set of text files in to a single list in python

I have a set of text files and i am trying to read it into a single list.but when i execute my code

def get_documents():
path1 = "D:/set/"
texts=[]
listing1 = os.listdir(path1)
for file in listing1:
with open(path1+file,'r') as f:
lines = f.read().splitlines()
texts.append(lines)
print texts


I am getting output as list of lists

[['Wanna see Riya Somani :) wish lyf olso moment lyk end half galfrnd... :) '], ['Worst book Mr. Chetan Bhagat.. Plz better stori ']]


How can i get it as a single list?

Answer

I believe instead of this:

texts.append(lines)

Do this:

texts.extend(lines)
Comments