Python Relative Path Simplifier
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 a
Solution 1:
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'
Solution 2:
I would use os.path.normpath
(+1 @unutbu), but just for fun, here's a way to do it manually:
def normpath(path3):
path = path3.split('/') # or use os.path.sep
answer = []
for p inpath:
if p != '..':
answer.append(p)
else:
if all(a=='..'for a in answer):
answer.append(p)
else:
answer.pop()
return'/'.join(answer)
And the output:
In [41]: normpath("../../../../../../../Source/lib/Target/abcd/abc_def_ghf/source/zzzModule/../../../../../../../../Source/directory/Common/headerFile.h")
Out[41]: '../../../../../../../../Source/directory/Common/headerFile.h'
Post a Comment for "Python Relative Path Simplifier"