[Tutor] if os.path.exists() or if not os.path.exists()?

2011-03-24 Thread Susana Iraiis Delgado Rodriguez
OK!! I see the problem now!
I also did a little change to the code,  I open the .txt before the for
stamentet. The script is working properly
gdal.AllRegister()
file_list = [
folders = Non
for root, folders, files in os.walk( "C:\\" ):
   file_list.extend(os.path.join(
root,fi) for fi in files if
fi.endswith(".shp"))
f = open('csv_pruebita.csv', 'wb')
log = open ('errors.txt','wb')
writer = csv.writer(f)
ruta = 'Ruta'
archivo = 'archivo'
prj = '.prj'
proyeccion = 'proyeccion'
campos = [ruta,archivo,prj,proyeccion]
writer.writerow(campos)

for row, filepath in enumerate(file_list, start=1):
#Partir la ruta del archivo en dos, ruta y nombre del archivo
(ruta, filename) = os.path.split(filepath)
#Escribir el nombre del archivo
shapeData = ogr.Open(filepath)
shp = 'Error al abrir el archivo' +filepath
#Abrir archivo de errores
if shapeData is None:
print shp
log.write(shp+"\n")
#Obtener la capa del shape
else:
..
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if os.path.exists() or if not os.path.exists()?

2011-03-24 Thread Tom Zych
On Thu, 24 Mar 2011 09:22 -0600, "Susana Iraiis Delgado Rodriguez"
 wrote:
> #This block is wrong, I think here is the problem
> if os.path.exists(shx):
> print 'El archivo ' +shx +' existe'
> else:
> log.write('No existe el archivo ' +shx + "\n")
> if os.path.exists(dbf):
> print 'El archivo ' +dbf +' existe'
> log.write('No existe el archivo ' +dbf + "\n")

I don't know about the rest, but you're missing an "else:" between the
last two lines.

-- 
Tom Zych / freethin...@pobox.com
"Because if they didn't vote for a lizard," said Ford, "the wrong lizard
might get in." -- DNA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] if os.path.exists() or if not os.path.exists()?

2011-03-24 Thread Susana Iraiis Delgado Rodriguez
Hello everyone!

I'm doing a python script that walks through files in my PC. Basically I'm
searching files that end up with .shp, but these files must have extensions
with .shx and .dbf. I want to write a .txt document which tells the user if
a file doesn't exist in the directory. But I haven't been sucessful, I got
the txt with wrong data, it shows an existing file.

import os, csv, time, socket
from osgeo import ogr,gdal,osr
from dbf import *

gdal.AllRegister()
file_list = [
folders = Non
for root, folders, files in os.walk( "C:\\" ):
file_list.extend(os.path.join(root,fi) for fi in files if
fi.endswith(".shp"))
writer = csv.writer(open('csv_pruebita.csv', "wb"))
ruta = 'Ruta'
archivo = 'archivo'
prj = '.prj'
proyeccion = 'proyeccion'
campos = [ruta,archivo,prj,proyeccion]
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' +filepat
log = open ("errors.txt","w+")
if shapeData is None:
print shp
log.write(shp + "\n")
else:
#I splitted the filepath because I need the same filepath but with different
extension
n = os.path.splitext(filepath)
p = n[0]+'.prj'
shx = n[0]+'.shx'
dbf = n[0]+'.dbf'
filepath = ''+filepath+''
filename = ''+filename+''
#This block is wrong, I think here is the problem
if os.path.exists(shx):
print 'El archivo ' +shx +' existe'
else:
log.write('No existe el archivo ' +shx + "\n")
if os.path.exists(dbf):
print 'El archivo ' +dbf +' existe'
log.write('No existe el archivo ' +dbf + "\n")
#This block works properly
if os.path.exists(p):
prj_text = open(p, 'r').read()
prjtext = ''+prj_text+''
aRow= [ filepath, filename, 1, prjtext]
writer.writerow(aRow)
else:
no_prj = 'Sin prj, no se puede determinar la proyeccion'
aRow1= [ filepath, filename, 0, no_prj]
writer.writerow(aRow1)
log.close()
print "El archivo esta listo"
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor