On 13/09/11 16:41, Susana Iraiis Delgado Rodriguez wrote:
Hi!
I just want to look the pathfile like this: C:\Python26 instead of
C:/\Python26,

OK, Let's see how you might debug that.

I feel the loop repeats its walking with this pathfile

I'm not sure what you mean by that but I do have some
comments below that might be relevant...

structure. About the indention for the code, I tried my best to make it
clear ande neat. But the mi e-mail editor it's mixing-up spaces.

OK, That's unfortunate. Which editor/email tool are you using?
We may be able to suggest alternate settings.

dir = ""
extn = ""
csv_name = ""
filesystemencoding = sys.getfilesystemencoding()

def directorio():
  global dir
  print 'Seleccione directorio donde empezar'
  dirname1 =
tkFileDialog.askdirectory(parent=root,initialdir="/",title='Selecciona
la ruta a escanear')

Does changing the '/' here have any affect on your final result?
Is this why you get the /\ maybe?

  if len(dirname1 ) > 0:
   print "You chose %s" % dirname1
  dir = dirname1

What gets printed here? Does it have the same /\ structure?

def extension():...

I don't think the above function is relevant so we can
ignore the code for now.

def csv_w():....

Same here, it just gets a filename - we could substitute
a dummy one while testing...


But this next function is way too big!
Try breaking it into chunks each in their own function.
It will make it easier to read and thus to understand
and to debug.


def buscar():
  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))

This could be put in a function to build the file list.
Also you don't need the assignment to folders since os.walk will do that for you and you don't use it anywhere anyway.

  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'

Frankly there is no real advantage putting these strings in variables. You only use them in one place so just use the literal strings in the code there. It will make the code much shorter and therefore easier to read!


  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):

Since you don't seem to use row anywhere you could just make this:

for filepath in file_list:

   directorio = os.path.dirname(filepath)
   filename = os.path.basename(filepath)
   shapeData = ogr.Open(filepath.encode(filesystemencoding))
   if shapeData is None:
    shp = 'Error al abrir el archivo ' + filepath.encode(filesystemencoding)

Since the rest of this is irrelevant to your problem we can
delete it for now...

    def sizeof_fmt(num):
     for x in ['bytes','KB','MB','GB','TB']:
      if num < 1024.0:
       return "%3.1f%s" % (num, x)
      num /= 1024.0
         #Obtener usuario

It's generally better to define all functions outside of
other functions unless you plan on returning them as
function objects. And if you are going to define a function inside another one please do it at the top and not in the middle of the function code.

    usuario = os.environ.get("USERNAME")
    user = ''+usuario+''
    host = socket.gethostname()
    maquina = ''+host+''

I don't understand what this is trying to do?
It adds an empty string to the user and host names?
You might also want to check that you get valid values
for these variables before using them...

    if os.path.exists(shx):
     print '.'
     shx1 = os.path.getsize(shx)
      #kb2 = sizeof_fmt(shx1)
    else:
     log.write('No existe el archivo ' +shx+"\n")

    if os.path.exists(dbf):
     print '.'
    else:
     log.write('No existe el archivo ' +dbf+"\n")

I'm not sure what this is for? The paths don't exist
so you log the error but then continue with the
function anyway?

    if os.path.exists(p):
                 #Si existe, asignar 1
     p1 = os.path.getsize(p)
     total = sizeof_fmt(s+shx1+p1)

If shx didn't exist shx1 will not exist and the code
will fail here I think.

     aRow= [ directorio, filename, x1, x2, y1, y2, geo, cuenta, 1,
prjtext, crea, modificacion, acceso, total, maquina, user]
     writer.writerow(aRow)

Could you simplify what you write to get rid of the extra data and see if the fault remains?


    else:
                 #Sino asignar 0
                         #no_prj = 'Sin prj, no se puede determinar la
proyeccion'
     total = sizeof_fmt(s+shx1)
     aRow1= [ directorio, filename, x1, x2, y1, y2, geo, cuenta, 0,
prjtext, crea, modificacion, acceso, total, maquina, user]
     writer.writerow(aRow1)
  log.close()
  f.close()
  print "El archivo esta listo"

I doubt the rest of the code contributes to the error
so I didn't read it...


root = Tk()
top = Toplevel() #Llamo una nueva ventana #Dimensiones de la ventana
root.minsize(400,200) #Barra de herramientas
toolbar = Frame(root) #Botones
b = Button(toolbar, text="Selecciona ruta", width=15, command=directorio)
b.pack(side=LEFT, padx=2, pady=2)
b = Button(toolbar, text="Escriba extension", width=15, command=extension)
b.pack(side=LEFT, padx=2, pady=2)
b = Button(toolbar, text="Nombre de csv", width=15, command=csv_w)
b.pack(side=LEFT, padx=2, pady=2)
b = Button(toolbar, text="Aceptar", width=6, command=buscar)
b.pack(side=LEFT, padx=2, pady=2)
toolbar.pack(side=TOP, fill=X)
root.mainloop()

HTH,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to