[Tutor] WRITING XLS FROM OS.WALK()
David Hutto
smokefloat at gmail.com
Sat Oct 9 06:15:35 CEST 2010
On Fri, Oct 8, 2010 at 11:55 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, 9 Oct 2010 06:34:44 am Susana Iraiis Delgado Rodriguez wrote:
>> 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,
>
>
> I'm sorry to tell you that you've just reinvented the wheel. This was
> already solved, a long, long time ago. It is called the glob module:
Hey, buddy pal. Isn't it true that newbs should take of advantage of
the fact that you have to solve the problem pythonically(as in by
yourself), even if the function already exists? If I get the gist of
thinikig like a programmer.
>
>>>> import glob
>>>> glob.glob("/home/steve/*.jpg")
> ['/home/steve/hoversonic.jpg', '/home/steve/seperated_at_birth.jpg']
>>>> glob.glob("/home/steve/*.txt")
> ['/home/steve/woss.txt', '/home/steve/file.txt', '/home/steve/post.txt']
>
>
> You should use that. It works, it is tested and thoroughly debugged, and
> it is powerful.
Certainly so, but not as powerful as the individual's ingenuity in
solving the problem at hand without foreknowledge of the 'known'
solution.
>
>
>> but now I want to write an excel file from it. The file
>> should show the full path from the found files.
>
>
> Excel files are a proprietary, secret, binary file format. There is a
> Python project to allow reading and writing Excel files, but since it
> has to reverse-engineer the secret format, there's no guarantee that it
> will work. Having said that, I believe that it is very reliable, but
> I've never used it myself.
>
> Google on "python read write excel files" for more information.
>
> However, if your only aim is to make the data available to Excel, and
> you don't care what sort of file you use, the best way is the standard
> interchange format between spreadsheet applications, the comma-
> separated value file, or CSV. This is a plain-text file, and Python
> comes with a module for reading and writing them. From the interactive
> interpreter, run this for more information:
>
> import csv
> help(csv)
>
>
>
>> This is the code:
>>
>> import os
>> a = open ("directorio.xls","w")
>
> Just because you name a file .xls doesn't make it an actual Excel file,
> any more than taking a JPEG and renaming it "word.exe" would turn it
> into the Microsoft Word application.
>
>
>> 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") ]
>
>
> This isn't your actual code. I know this, because the indentation is
> broken and it gives a syntax error.
>
>> for f in filelist:
>> allfiles.append(f)
>
> This is better written as:
>
> allfiles.extend(filelist)
>
>
> but of course it is better to use the glob module.
>
>> for i in allfiles:
>> print i
>> a.write(i)
>> a.write("\n")
>
> The name "i" is normally used for integers, not file names. It would be
> better to write that as:
>
> for filename in allfiles:
> print filename
> a.write(filename + '\n')
>
>
>
> --
> Steven D'Aprano
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
No problems here yet though buddy.
More information about the Tutor
mailing list