<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Apr 22, 2015 at 9:18 AM,  <span dir="ltr"><<a href="mailto:subhabrata.banerji@gmail.com" target="_blank">subhabrata.banerji@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Dear Group,<br>
<br>
I am trying to open a bunch of files from a directory and trying to put the results in list of lists that is to say,<br>
<br>
that is to say,<br>
I have a list of file names of a directory, I want to read each one of them.<br>
After reading each one of them, I want to put the results of each file in a list.<br>
These lists would again be inserted to create a list of lists.<br>
<br>
to do this I am trying to write it as follows:<br>
<br>
list_of_files = glob.glob('C:\Python27\*.*')<br>print list_of_files<br>list1=[]<br>list2=[]<br>list_N=[list1,list2]<br>for i,j in zip(list_of_files,list_N):<br>    print i,j<br>    x1=open(i,"r").read()<br>    x2=j.append(x1)<br>all_sent=list_N<br>print all_sent<br></blockquote><div><br></div><div>In the original e-mail, it looked like you had an indentation error, which I think I corrected correctly, though you may have been missing a line. A few comments with your code:</div><div>First, as you only create two lists, it would only ever iterate twice, and would ignore the rest of the files.</div><div>Secondly, you only ever append a single item to the lists.</div><div>Third, list.append always returns None, so the line "x2 = j.append(x1)" will always result in x2 being None. Not that it matters much as you never use x2 in the code.</div><div><br></div><div>Presuming that you actually are doing something in your real code that would be producing lists for each file dynamically, rather than just the string contents of the file, you probably should create the child lists in the loop, rather than outside. Something like:</div><div><br></div><div>results = [] # Create the results list for all files.</div><div>for path in glob.glob('C:\Python27\*.*'):</div><div>    fileResult = [] # Create the result for the file.</div><div>    with open(path, 'r') as myFile: # Note that this helps ensure the file is closed, even in more complicated cases, and in all implementations. It is not needed in such a simple case for CPython only, however.</div><div>        fileResult.append(myFile.read()) # Or whatever real processing you want to do. file.read() returns a single string, and fileResult in this example only ever has a single item, so it is a somewhat useless layer of abstraction.</div><div>    results.append(fileResult) # Add the file's result into the full list of results. </div><div><br></div><div>Note that, depending on the processing you are doing, and on what files, you may want to avoid reading all files into memory at once, and instead process them incrementally, preferably avoiding even reading all of a single file. This is particularly easy if processing the file by line.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Am I doing anything wrong? If any one may kindly suggest?<br>
Is there any smarter way to do it? I am using Python2.7+<br>
on MS-Windows 7 Professional Edition.<br>
Apology for any indentation error.<br>
<br>
Regards,<br>
Subhabrata Banerjee.<br></blockquote></div></div></div>