3

这个问题应该是不费吹灰之力的,但我还没有解决它。

我需要一个函数,它接受两个参数,每个参数都是相对或绝对的文件路径,并返回一个文件路径,它是相对于第二个路径(开始)解析的第一个路径(目标)。解析的路径可能是相对于当前目录的,也可能是绝对的(我不在乎)。

这里作为一个尝试的实现,完成了几个文档测试,它练习了一些示例用例(并演示了它失败的地方)。我的源代码存储库中也有一个可运行的脚本,但它可能会改变。如果没有提供参数,runnable 脚本将运行 doctest,或者如果提供了参数,则将一两个参数传递给 findpath。

def findpath(target, start=os.path.curdir):
    r"""
    Find a path from start to target where target is relative to start.

    >>> orig_wd = os.getcwd()
    >>> os.chdir('c:\\windows') # so we know what the working directory is

    >>> findpath('d:\\')
    'd:\\'

    >>> findpath('d:\\', 'c:\\windows')
    'd:\\'

    >>> findpath('\\bar', 'd:\\')
    'd:\\bar'

    >>> findpath('\\bar', 'd:\\foo') # fails with '\\bar'
    'd:\\bar'

    >>> findpath('bar', 'd:\\foo')
    'd:\\foo\\bar'

    >>> findpath('bar\\baz', 'd:\\foo')
    'd:\\foo\\bar\\baz'

    >>> findpath('\\baz', 'd:\\foo\\bar') # fails with '\\baz'
    'd:\\baz'

    Since we're on the C drive, findpath may be allowed to return
    relative paths for targets on the same drive. I use abspath to
    confirm that the ultimate target is what we expect.
    >>> os.path.abspath(findpath('\\bar'))
    'c:\\bar'

    >>> os.path.abspath(findpath('bar'))
    'c:\\windows\\bar'

    >>> findpath('..', 'd:\\foo\\bar')
    'd:\\foo'

    >>> findpath('..\\bar', 'd:\\foo')
    'd:\\bar'

    The parent of the root directory is the root directory.
    >>> findpath('..', 'd:\\')
    'd:\\'

    restore the original working directory
    >>> os.chdir(orig_wd)
    """
    return os.path.normpath(os.path.join(start, target))

从 doctest 中的注释可以看出,当 start 指定驱动器号并且目标相对于驱动器的根目录时,此实现失败。

这提出了几个问题

  1. 这种行为是 os.path.join 的限制吗?换句话说, os.path.join('d:\foo', '\bar') 是否应该解析为 'd:\bar'?作为一个 Windows 用户,我倾向于这样认为,但我不想认为像 path.join 这样的成熟函数需要修改来处理这个用例。
  2. 是否存在适用于所有这些测试用例的现有目标路径解析器(例如 findpath)的示例?
  3. 如果对上述问题“否”,您将如何实现这种期望的行为?
4

1 回答 1

3

我同意你的看法:这似乎是 os.path.join 中的一个缺陷。看起来你必须单独处理驱动器。此代码通过了您的所有测试:

def findpath(target, start=os.path.curdir):
    sdrive, start = os.path.splitdrive(start)
    tdrive, target = os.path.splitdrive(target)
    rdrive = tdrive or sdrive
    return os.path.normpath(os.path.join(rdrive, os.path.join(start, target)))

(是的,我必须嵌套两个 os.path.join 才能让它工作......)

于 2009-10-31T15:50:39.840 回答