简体   繁体   English

有关Python中路径的问题

[英]Question about paths in Python

let's say i have directory paths looking like this: 假设我的目录路径如下所示:

this/is/the/basedir/path/a/include
this/is/the/basedir/path/b/include
this/is/the/basedir/path/a
this/is/the/basedir/path/b

In Python, how can i split these paths up so they will look like this instead: 在Python中,我如何分割这些路径,以便它们看起来像这样:

a/include
b/include
a
b

If i run os.path.split(path)[1] it will display: 如果我运行os.path.split(path)[1],它将显示:

include
include
a
b

What should i be trying out here, should i be looking at some regex command or can this be done without it? 我应该在这里尝试什么,应该查看一些regex命令还是可以不使用它来完成? Thanks in advance. 提前致谢。

EDIT ALL: I solved it using regular expressions, damn handy tool :) 编辑全部:我使用正则表达式解决了该死的方便工具:)

Perhaps something like this, depends on how hardcoded your prefix is: 也许像这样,取决于前缀的硬编码方式:

def removePrefix(path, prefix):
    plist = path.split(os.sep)
    pflist = prefix.split(os.sep)
    rest = plist[len(pflist):]
    return os.path.join(*rest)

Usage: 用法:

print removePrefix("this/is/the/basedir/path/b/include", "this/is/the/basedir/path")
b/include

Assuming you're on a platform where the directory separator ( os.sep ) really is the forward slash). 假设您使用的平台上的目录分隔符( os.sep )确实是正斜杠。

This code tries to handle paths as something a little more high-level than mere strings. 此代码尝试将路径作为比纯字符串更高级的东西来处理。 It's not optimal though, you could (or should) do more cleaning and canonicalization to be safer. 尽管这不是最佳选择,但您可以(或应该)进行更多清洁和标准化操作,以确保安全。

what about partition ? 分区呢?
It Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. 它在第一次出现sep时分割字符串,并返回一个三元组,其中包含分隔符之前的部分,分隔符本身以及分隔符之后的部分。 If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings. 如果找不到分隔符,则返回一个包含字符串本身的3元组,然后是两个空字符串。

data = """this/is/the/basedir/path/a/include
this/is/the/basedir/path/b/include
this/is/the/basedir/path/a
this/is/the/basedir/path/b"""
for line in data.splitlines():
    print line.partition("this/is/the/basedir/path/")[2]

#output
a/include
b/include
a
b

Updated for the new comment by author: 更新了作者的新评论:
It looks like u need rsplit for different directories by whether the directory endswith "include" of not: 看来您需要根据目录是否以“ include”结尾而不是rsplit不同的目录:

import os.path
data = """this/is/the/basedir/path/a/include
this/is/the/basedir/path/b/include
this/is/the/basedir/path/a
this/is/the/basedir/path/b"""
for line in data.splitlines():
    if line.endswith('include'):
        print '/'.join(line.rsplit("/",2)[-2:])
    else:
        print os.path.split(line)[1]
        #or just
        # print line.rsplit("/",1)[-1]
#output
a/include
b/include
a
b

Maybe something like this: 也许是这样的:

result = []

prefix = os.path.commonprefix(list_of_paths)
for path in list_of_paths:
    result.append(os.path.relpath(path, prefix))

This works only in 2.6. 仅在2.6中有效。 The relapath in 2.5 and before does the work only in case the path is the current working directory. 2.5及之前版本中的relapath仅在路径为当前工作目录的情况下才起作用。

While the criterion is not 100% clear, it seems from the OP's comment that the key issue is specifically whether the path's last component ends in "include". 尽管该标准不是100%明确的,但从OP的评论看来,关键问题特别是路径的最后一个组件是否以“ include”结尾。 If that is the case, and to avoid going wrong when the last component is eg "dontinclude" (as another answer does by trying string matching instead of path matching), I suggest: 如果是这种情况,并且为了避免在最后一个组件为例如“ dontinclude”时出错(就像另一个答案是尝试使用字符串匹配而不是路径匹配),我建议:

def lastpart(apath):
    pieces = os.path.split(apath)
    final = -1
    if pieces[-1] == 'include':
        final = -2
    return '/'.join(pieces[final:])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM