Python relative path simplifier

path, python, windows

Solution

Use os.path.normpath to resolve `..` in the path:

In [93]: os.path.normpath(os.path.join(path1,path2))
Out[93]: '../Source/directory/Common/headerFile.h'

Problem

I am having issues with paths, hitting the Windows' restriction on the number of characters in the path at 256. In some place in my python script the 2 paths are getting appended and they both are relative paths, and these become very long: e.g.: ``` path1 = "..\\..\\..\\..\\..\\..\\..\\Source/lib/Target/abcd/abc_def_ghf/source/zzzModule/" path2 = "../../../../../../../../Source/directory/Common/headerFile.h" ``` Appended path: ``` path3 = "..\\..\\..\\..\\..\\..\\..\\Source/lib/Target/abcd/abc_def_ghf/source/zzzModule/../../../../../../../../Source/directory/Common/headerFile.h" ``` And `path3` is passed in my Visual Studio solution. At this point VS stops and says that the file is not found. The observation here is that the final `path3` goes 7 levels up then 7 levels down and then again 8 levels up. Is there any utility in python which will take this and generate a simplified relative path for me? e.g. ``` some_utility(path3) = "../../../../../../../../Source/directory/Common/headerFile.h" ``` I know I can write a utility myself but I am just checking if there is any. If there is some it will save my 20 minutes of coding.

Original source