[Qgis-developer] Patch for Table Manager (ver.0.17) - python plugin

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Jun 4 06:55:11 EDT 2009


>         srcPath += '/' + srcName

>     if self.writeToShp(tmpDir+'/'+srcName+'.shp', self.provider.encoding())

>       QgsVectorFileWriter.deleteShapeFile(tmpDir+'/'+srcName+'.shp')

>     QFile(srcPath+'.dbf~').remove()

>     if not QFile(srcPath+'.dbf').rename(srcPath+'.dbf~'):

>       QgsVectorFileWriter.deleteShapeFile(tmpDir+'/'+srcName+'.shp')

>     shutil.copy(tmpDir+'/'+srcName+'.dbf', srcPath+'.dbf')
>     QFile(srcPath+'.dbf~').remove()

 ouch ouch ouch!

 If you need to do file path manipulation in python then it's
generally better to use functions from the os.path module - it handles
OS-specific things like / and \, splitting dir/filename from paths,
creating canonical paths, resolving symlinks (if your OS supports it)
and all that.

 Note that os.path tends to fail if you feed it QStrings, so convert
to python strings beforehand, or maybe use methods from QDir -
actually, that's probably a better idea! QDir has methods for these
sort of things too. Concatenating strings for files, especially adding
slashes or backslashes, can get painful when you go across platforms.

Barry


More information about the Qgis-developer mailing list