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

Peter Otten __peter__ at web.de
Wed Nov 24 19:36:32 CET 2010


Susana Iraiis Delgado Rodriguez wrote:

> Hello memebers:
> 
> I'm writing a python script to validate if files with extension .prj
> exist, if they exist it should write 1 or 0 into an excel cell. I've
> working to do this properly, but I'm not getting the results I need. The
> script doesn't find all the files, is like the files don't exist but they
> exist, besides I think is just reading some of the files that are in
> C:\Python 26. I really need a hand on this.
> 
> import os, time, socket, pylab
> from xlwt import Workbook
> from osgeo import ogr,gdal,osr
> from dbf import *
> #Register GAL drivers
> gdal.AllRegister()
> #Create an empty list
> file_list = []
> folders = None
> #Code to indicate directory
> for root, folders, files in os.walk( "C:\\" ):
>  file_list.extend(os.path.join(root,fi) for fi in files if
> fi.endswith(".shp"))
> #Open excel book
> wrkbk = Workbook()
> #Add excel sheet
> 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 = n[0]+'.prj'

Add

   print "looking for", p

here. Does it show what you expect/want? In what directory will lexists() 
look for the file?

>  if os.path.lexists(p):

lexists()' claim to fame is that it "Returns True for broken symbolic 
links". Are you sure you prefer that over good old os.path.exists()?

>       wksht.row(row).write(2, 1)
>  else:
>       wksht.row(row).write(2, 0)
> wrkbk.save('shp3.xls')




More information about the Tutor mailing list