tags:

views:

24

answers:

1

How could this code fragment...

def subInPath(origPath, subPath):
    origSplit = split(origPath, '/')
    subSplit = split(subPath, '/')

    subRoot = subSplit[0]
    origSplit.reverse()
    print origSplit.index(subRoot)
    rootIndex = origSplit.index(subRoot)

    origSplit[:rootIndex+1] = []
    origSplit.reverse()

    newPath = join(origSplit, sep)
    newPath += (sep + subPath)

    if not exists(newPath):
        raise Exception, "Path subbed in not found."
    return newPath

with the arguments ("C:/Users/MyName/Desktop/second_stage/Kickle_Pack/GardenLand_D.xml", "Kickle_Pack/Animations/TileAnims_48x48.xml")...

Output 2 at the print statement, but throw a ValueError at the statement below it. I'm baffled.

A: 

Always use os.path module when working with directories or paths. It's got all the methods needed to work with directories, plus it has the advantage of being compatible in multiples operating system.

It's just better software engineering.

MovieYoda
I wasn't able to find what I am trying to accomplish via it.
random
Fixed, was an issue with the os.sep at the bottom.
random
MovieYoda
Yes I did, I tried, and I completely agree with you. However I wasn't able to find what I needed to accomplish (well I'm sure I could have used what I found to accomplish it, but it would have been going around the mulberry bush). Also, note that I am not able to use any of the functions that aren't cross-platform (there are several in the os.path module). This gets the job done, and in the end that is what matters (though I definitely do agree with you, and if you know of a cleaner way via that module let me know).
random
@random: """Fixed, was an issue with the os.sep at the bottom""" ??? The mention of `sep` (*not* `os.sep`) is *AFTER* the `origSplit.index(subRoot)` lines!!! Please explain.
John Machin