[Tutor] 'function' object has no attribute 'writer'

Susana Iraiis Delgado Rodriguez susana.delgado_s at utzmg.edu.mx
Tue Sep 6 17:47:49 CEST 2011


Hello Petter!!
Thank you for answer to my question. I apologize for the writing of my code,
won't happen again. I added the lines you suggested me, and the script work
fine. Than you!! Here's the code corrected and indented.

from Tkinter import * #Llamo las librerias graficas de Tk
import tkSimpleDialog #Libreria de almacenamiento de dialogos
import tkMessageBox #Libreria de mensajes
import tkFileDialog
import sys, os, csv, time, socket, stat
from osgeo import ogr,gdal,osr
from osgeo.gdalconst import *
from PIL import Image
dir = ""
extn = ""
csv_name = ""
filesystemencoding = sys.getfilesystemencoding()

def directorio():
    global dir
    print 'Seleccione directorio donde empezar'
    dirname =
tkFileDialog.askdirectory(parent=root,initialdir="/",title='Selecciona la
ruta a escanear')
    if len(dirname ) > 0:
      print "You chose %s" % dirname
    dir = dirname
def extension():
    global extn
    print "Escribe la extension que necesitas buscar"
    ext=tkSimpleDialog.askstring('Extension a buscar:','')
    print 'Buscando archivos: ',ext
    extn = ext
def csv_w():
    global csv_name
    print "Nombre del csv a crear"
    inv=tkSimpleDialog.askstring('Nombre de .csv:','')
    print 'Archivo de salidad: ',inv
    csv_name = inv
def boton4():
    print 'Iniciando...'
    gdal.AllRegister()
    file_list = []
    folders = None
    for root, folders, files in os.walk(dir):
       file_list.extend(os.path.join(root,fi) for fi in files if
fi.endswith(extn))
    f = open(csv_name, 'wb')
    log = open ('log_errores.txt','w')
    writer = csv.writer(f)
    ruta = 'Ruta'
    archivo = 'archivo'
    x_min = 'x_min'
    x_max = 'x_max'
    y_min = 'y_min'
    y_max = 'y_max'
    geometria = 'geometria'
    num_elem = 'num_elem'
    prj = '.prj'
    proyeccion = 'proyeccion'
    fecha = 'fecha_modificacion'
    creacion = 'fecha_creacion'
    ultimo = 'ultimo_acceso'
    tamanio = 'tamanio_aprox'
    maq = 'maquina_host'
    usu = 'usuario'
    campos =
[ruta,archivo,x_min,x_max,y_min,y_max,geometria,num_elem,prj,proyeccion,creacion,fecha,ultimo,tamanio,maq,usu]
    writer.writerow(campos)
    for row, filepath in enumerate(file_list, start=1):
      directorio = os.path.dirname(filepath)
      filename = os.path.basename(filepath)
      shapeData = ogr.Open(filepath.encode(filesystemencoding))
     shp = 'Error al abrir el archivo' +filepath
......

2011/9/5 Peter Otten <__peter__ at web.de>

> Susana Iraiis Delgado Rodriguez wrote:
>
> Susana, please use 4-space indents for your code samples. I've said it
> before, but I think it's worthwhile repeating because it makes it much
> easier to grasp the structure of a script at first sight.
>
> > But now I get a differente error, this script is going to collect
> > information from shapefiles, I asked the user where to start the search,
> > the file extension, and the csv name. Now I get:
> >>>> import win
> >>>> Seleccione directorio donde empezar
> > You chose C:/
> > Escribe la extension que necesitas buscar
> > Buscando archivos:  .shp
> > Nombre del csv a crear
> > Archivo de salidad:  gui.csv
> > Iniciando...
> > Exception in Tkinter callback
> > Traceback (most recent call last):
> >   File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
> >     return self.func(*args)
> >   File "win.py", line 65, in boton4
> >     shapeData = ogr.Open(filepath)
> >   File "C:\Python26\lib\site-packages\osgeo\ogr.py", line 4033, in Open
> >     return _ogr.Open(*args, **kwargs)
> > TypeError: in method 'Open', argument 1 of type 'char const *'
> >
> > The other part of the code is:
>
> > from osgeo import ogr,gdal,osr
>
> >   shapeData = ogr.Open(filepath)
>
> filepath is a unicode string, your library seems to expect byte strings.
> Try converting it like so:
>
> import sys
> filesystemencoding = sys.getfilesystemencoding()
> #...
> shapeData = ogr.Open(filepath.encode(filesystemencoding))
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110906/ad6a16b8/attachment.html>


More information about the Tutor mailing list