Ceem Ceem - 1 year ago 93
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/"
listing1 = os.listdir(path1)
for file in listing1:
with open(path1+file,'r') as f:
lines = f.read().splitlines()
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 Source

I believe instead of this:


Do this:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download