[Tutor] Run application from MS-DOS with sys.argv

Susana Iraiis Delgado Rodriguez susana.delgado_s at utzmg.edu.mx
Tue Mar 29 22:38:42 CEST 2011


Hello List:

I developed a script to walk through a specific directory in my PC and look
for files with the same extension (.shp). I want the user to enter from
MS-DOS and write the the directory, file extension and csv filename.
My script reads the arguments from Windows console, but when I opened the
txt file and csv file I noticed that isn't walking through all the root I
wrote in MS-DOS. This is my module:

import os, csv, time, socket, sys
from osgeo import ogr,gdal,osr
#This should be the order for the arguments('csv_args.py [root]
[file_extension] [csv filename]')
#The user is typing python csv_args.py C:\ .shp csv.csv
directorio = sys.argv[1]
extension = sys.argv[2]
csv_salida = sys.argv[3]
if len(sys.argv) == 4:
    print 'Iniciando...'
    gdal.AllRegister()
    file_list = []
    folders = None
    for root, folders, files in os.walk(directorio):
        file_list.extend(os.path.join(root,fi) for fi in files if
fi.endswith(extension))
    f = open(csv_salida, 'wb')
    log = open ('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'
    maq = 'maquina_host'
    usu = 'usuario'
    campos =
[ruta,archivo,x_min,x_max,y_min,y_max,geometria,num_elem,prj,proyeccion,fecha,maq,usu]
    writer.writerow(campos)
    for row, filepath in enumerate(file_list, start=1):
        (ruta, filename) = os.path.split(filepath)
        shapeData = ogr.Open(filepath)
        shp = 'Error al abrir el archivo' +filepath
        if shapeData is None:
            print shp
            log.write(shp+"\n")
        else:
            layer = shapeData.GetLayer()
            feature = layer.GetNextFeature()
            x_y = layer.GetExtent()
            x1 = x_y[0]
            x2 = x_y[1]
            y1 = x_y[2]
            y2 = x_y[3]
            defn = layer.GetLayerDefn()
            geo = defn.GetGeomType()
            cuenta = layer.GetFeatureCount()
            proy = layer.GetSpatialRef()
            prjtext = ''+str(proy)+''
            n = os.path.splitext(filepath)
            p = n[0]+'.prj'
            shx = n[0]+'.shx'
            dbf = n[0]+'.dbf'
            filepath = ''+filepath+''
            filename = ''+filename+''
            t = time.strftime("%m/%d/%Y %I:%M:%S
%p",time.localtime(os.path.getmtime(filepath)))
            modificacion = ''+t+''
            usuario = os.environ.get("USERNAME")
            user = ''+usuario+''
            host = socket.gethostname()
            maquina = ''+host+''
            if os.path.exists(shx):
                print 'El archivo ' +shx +' existe'
            else:
                og.write('No existe el archivo ' +shx+"\n")
            if os.path.exists(dbf):
                print 'El archivo ' +dbf +' existe'
            else:
                log.write('No existe el archivo ' +dbf+"\n")
            if os.path.exists(p):
                aRow= [ filepath, filename, x1, x2, y1, y2, geo, cuenta, 1,
prjtext, modificacion, maquina, user]
                writer.writerow(aRow)
            else:
                aRow1= [ filepath, filename, x1, x2, y1, y2, geo, cuenta, 0,
prjtext, modificacion, maquina, user]
                writer.writerow(aRow1)
    log.close()
    f.close()
    print "El archivo esta listo"
else:
    print "Tus argumentos no son correctos"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110329/65441d60/attachment-0001.html>


More information about the Tutor mailing list