[Tutor] Save file in a specific directory

Susana Iraiis Delgado Rodriguez susana.delgado_s at utzmg.edu.mx
Tue Dec 7 18:15:13 CET 2010


I make a script to redirect a txt file from an external directory, but in
this directory I don't have permission to write, just to read data. So I
make this module:
import os, time,fnmatch
from xlwt import Workbook
from osgeo import ogr,gdal,osr
from dbf import *
gdal.AllRegister()
file_list = []
folders = None
for root, folders, files in os.walk( "R:\\" ):
for filename in fnmatch.filter(files, '*.shp'):
    file_list.append(os.path.join(root, filename))
    wrkbk = Workbook()
    wksht = wrkbk.add_sheet('shp')
    wksht.row(0).write(0,'ruta')
    wksht.row(0).write(1,'archivo')
    wksht.row(0).write(2,'estructura bd')
    for row, filepath in enumerate(file_list, start=1):
        wksht.row(row).write(0, filepath)
        (ruta, filename) = os.path.split(filepath)
         wksht.row(row).write(1, filename)
         f = os.path.splitext(filename)
         t = f[0]+'_bd.txt'
         d = n[0]+'.dbf'
         if os.path.lexists(d):
               filepath = "C:\\Python26\\"
               a = open (filepath +t,"w+")
               dbf = Dbf(d,new=False)
               for fldName in dbf.fieldDefs:
    a.write(fldName.name)
    a.write(" || ")
    a.write(fldName.typeCode)
    a.write("\n")
   dbf.close()
   a.close()
    wksht.row(row).write(2, t)
   else:
   print "El archivo " +n[0]+".shp" " no tiene dbf"
   wksht.row(row).write(10, "Sin bd")


wrkbk.save('C\\Python26\\biblio_shp.xls')
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101207/15ddc1cd/attachment.html>


More information about the Tutor mailing list