Python相对路径简化器 [英] Python relative path simplifier

查看:151
本文介绍了Python相对路径简化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了路径问题,达到了Windows对路径中字符数的限制,为256.

I am having issues with paths, hitting the Windows' restriction on the number of characters in the path at 256.

在我的python脚本中的某些地方,两个路径都被追加了,它们都是相对路径,它们变得很长:

In some place in my python script the 2 paths are getting appended and they both are relative paths, and these become very long:

例如:

path1 = "..\\..\\..\\..\\..\\..\\..\\Source/lib/Target/abcd/abc_def_ghf/source/zzzModule/"
path2 = "../../../../../../../../Source/directory/Common/headerFile.h"

附加路径:

path3 = "..\\..\\..\\..\\..\\..\\..\\Source/lib/Target/abcd/abc_def_ghf/source/zzzModule/../../../../../../../../Source/directory/Common/headerFile.h"

然后path3在我的Visual Studio解决方案中传递.此时,VS停止并说找不到文件.

And path3 is passed in my Visual Studio solution. At this point VS stops and says that the file is not found.

这里的观察结果是最终的path3上升了7个级别,然后下降了7个级别,然后又上升了8个级别. python中是否有任何实用程序可以使用它并为我生成简化的相对路径?

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?

例如

some_utility(path3) = "../../../../../../../../Source/directory/Common/headerFile.h"

我知道我可以自己编写一个实用程序,但我只是在检查是否有实用程序.如果有的话,将节省我20分钟的编码.

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.

推荐答案

使用 os.path.normpath 解析路径中的..:

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

这篇关于Python相对路径简化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆