[Tutor] If os.path.lexists() isn't working properly

Susana Iraiis Delgado Rodriguez susana.delgado_s at utzmg.edu.mx
Wed Nov 24 21:55:11 CET 2010


Hello Peter!

I added the line you suggested me and found out that I was just searching
for the filenames without pointing to a specific directory, so Python took
its directory (Python26) as default. After that I need to add a '\' to
separate the path from the filename because it was reading them as a single
string. But finally it worked nicely! here is the corrected script.

import os, time, socket, pylab, fnmatch
from xlwt import Workbook
from osgeo import ogr,gdal,osr
from dbf import *

gdal.AllRegister()
file_list = []
folders = None
for root, folders, files in os.walk( "C:\\" ):
     for filename in fnmatch.filter(files, '*.shp'):
       file_list.append(os.path.join(root, filename))
wrkbk = Workbook()
wksht = wrkbk.add_sheet('shp')
wksht.row(0).write(0,'ruta')
wksht.row(0).write(1,'archivo')
wksht.row(0).write(2,'prj')
for row, filepath in enumerate(file_list, start=1):
 wksht.row(row).write(0, filepath)
 (ruta, filename) = os.path.split(filepath)
 wksht.row(row).write(1, filename)
 n = os.path.splitext(filename)
 p = ruta+'\\'+n[0]+'.prj'
#"ruta" is the variable that saves the pathfile
 print "looking for", p
#I changed to os.path.exists()
 if os.path.exists(p):
      wksht.row(row).write(2, 1)
 else:
      wksht.row(row).write(2, 0)
wrkbk.save('shp3.xls')

#Mensajes para mostar en la consola
SEARCH_PATH = os.getcwd()
TARGET_FILE = os.path.realpath('shp3.xls')
print "Buscando en", SEARCH_PATH, "and writing to", TARGET_FILE
print "Encontrando archivos..."
print "Escribiendo archivo de Excel..."
print "Listo."

Thank you so much fro your help!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101124/ea6c2ae1/attachment.html>


More information about the Tutor mailing list