rhexter

Working with paths in cityengine python script execution - Tip

Discussion created by rhexter on Jul 25, 2012
This may help others if you come across odd behaviour.
I'm running on win7.
I was having a number of issues breaking a path up in script execution, the confusing thing is that it works fine in the console, so:

#in CE python console
import os.path
myPath="c:\\assets\\buildings\\downtown\\building_A.obj"
assetName=os.path.split(myPath)[1]
assetName=os.path.splitext(assetName)[0]

#or if your pythonic-ally one line inclined:

assetName=os.path.splitext(os.path.split(myPath)[1])[0]



Doing the same in script execution was not working and I couldn't figure out why i wasn't getting the same result's.
SO make sure you switch your backslashes to forward slashs, unix style, for paths that are handled in scripts if you want to use os.path operations:

myPath="c:\\assets\\buildings\\downtown\\building_A.obj"
myPath=myPath.replace("\\","/")
assetName=os.path.splitext(os.path.split(myPath)[1])[0]



Magic now I get the results I'm expecting.

Outcomes