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

James Reynolds eire1130 at gmail.com
Mon Sep 5 18:21:21 CEST 2011


On Mon, Sep 5, 2011 at 12:08 PM, Susana Iraiis Delgado Rodriguez <
susana.delgado_s at utzmg.edu.mx> wrote:

> I want to write a csv file, but I need the final user to give me some
> values to open and update the file. My code is:
> 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 = ""
> 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():
>  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)
> ....... more code
> But when I run it,console shows:
> Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 b
> win32
> Type "help", "copyright", "credits" or "license" for more informa
> >>> 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 43, in boton4
>     writer = csv.writer(open(csv_name, 'w'))
> AttributeError: 'function' object has no attribute 'writer'
> I read a tutorial and csv has a writer function, I'm lost
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


You have a function called "csv()" in your code.

So when you go csv.blahbittyblob, you've overriden your imported module
called "csv" with the function called "csv", python then tries to look up
the attribute blahbittyblob and can't find it. it then throws an error.

Change your csv function to be csvx() or _csv or something other than csv
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110905/e739e004/attachment-0001.html>


More information about the Tutor mailing list