4

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.

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Dec 19, 2013 at 23:00
2
  • Googling for python path normalize brings answer in the first link – docs.python.org/library/os.path.html Commented Dec 20, 2013 at 12:16
  • @PiotrDobrogost : I dodn't know that it is called path normalization. I had searched for "Python relative path optimization" but the results didn't help. Commented Dec 20, 2013 at 19:02

2 Answers 2

9

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'
answered Dec 19, 2013 at 23:05
Sign up to request clarification or add additional context in comments.

Comments

1

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 in path:
 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'
answered Dec 19, 2013 at 23:15

1 Comment

This misses some important edgecases; windows paths can start with \\.` or \\?`, which must be preserved unchanged, you can have UNC paths (start with //machine/mountpoint/...) and you can have drive letters (C:/...), and you are not processing ./ elements (current directory). You also want to use path3.replace('\\', '/') to handle either forward or backward slashes the same.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.