[Tutor] WRITING XLS FROM OS.WALK()

Emile van Sebille emile at fenx.com
Sat Oct 9 00:05:52 CEST 2010


On 10/8/2010 12:34 PM Susana Iraiis Delgado Rodriguez said...
> Hello members:
> I developed a Python module to make a list which contains all the files
> ending with .shp and .dbf extensions, I have solved this already, but now I
> want to write an excel file from it. The file should show the full path from
> the found files. This is the code:
>
> import os
> a = open ("directorio.xls","w")
> allfiles = [] #store all files found
>       for root,dir,files in os.walk("C:\\"):
>             filelist = [ os.path.join(root,fi) for fi in files if
> fi.endswith(".shp") or fi.endswith(".dbf") ]
>             for f in filelist:
>                  allfiles.append(f)
> for i in allfiles:
>        print i
>        a.write(i)
>        a.write("\n")
>
> With the code above, I have the print and the .xls file with
> this information in it, the issue here is that my file doesn't have the
> complete information that I got in the console. Any idea? The last line from
> excel is C:\Python26
>

You may find that finishing with a.flush() and a.close() fixes your problem.

Also, it appears that you're doing more than is required -- 
specifically, looping through filelist appending its members to allfiles 
can be done more simply with append, so where you're saying:

     filelist = [...

you could say

     allfiles.extend([...

and forget about filelist entirely.

HTH,

Emile




More information about the Tutor mailing list