在Windows中查找相对于另一个的路径 [英] Find a path in Windows relative to another

查看:112
本文介绍了在Windows中查找相对于另一个的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

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

  def findpath (target,start = os.path.curdir):
r
找到一个从开始到目标相对于开始的路径

>>> ; orig_wd = os.getcwd()
>>> os.chdir('c:\\windows')#所以我们知道工作目录是什么

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

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

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

>>> findpath('\\\\'','d:\ \foo')#失败,'\\\\''
'd:\\\\''

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

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

>>>> findpath('\\baz','d:\\ \\foo\\\'')#失败'\\baz'
'd:\\ baz'

因为我们在C盘findpath可能被允许为同一个驱动器上的目标返回
的相对路径。我用abspath
确认最终的目标是我们所期望的。
>>> os.path.abspath(findpath('\\'))
'c:\\\\''

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

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

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

根目录的父目录是根目录。
>>> findpath('..','d:\\')
'd:\\'

恢复原来的工作目录
>>> ; os.chdir(orig_wd)

return os.path.normpath(os.path.join(start,target))

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



这带来了一些问题


  1. 这是os.path的限制。加入?换句话说,如果os.path.join('d:\ foo','\ bar')解析为'd:\ bar'?作为Windows用户,我倾向于这样认为,但是我讨厌像一个成熟的函数,如path.join需要改变来处理这个用例。
  2. 是否有一个现有的目标路径解析器的例子,如findpath,将在所有的这些测试用例?

  3. 如果对上述问题否,您将如何实现这个所需的行为?



$ $ $ $ $ $ c $ def findpath(target,start = os.path.curdir)
sdrive ,start = os.path.splitdrive(start)
tdrive,target = os.path.splitdrive(target)
rdrive = tdrive或者sdrive
return os.path.normpath(os.path .join(rdrive,os.path.join(start,target)))



<是>我不得不嵌套两个os.path.join来使它工作...)

This problem should be a no-brainer, but I haven't yet been able to nail it.

I need a function that takes two parameters, each a file path, relative or absolute, and returns a filepath which is the first path (target) resolved relative to the second path (start). The resolved path may be relative to the current directory or may be absolute (I don't care).

Here as an attempted implementation, complete with several doc tests, that exercises some sample uses cases (and demonstrates where it fails). A runnable script is also available on my source code repository, but it may change. The runnable script will run the doctest if no parameters are supplied or will pass one or two parameters to findpath if supplied.

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))

As you can see from the comments in the doctest, this implementation fails when the start specifies a drive letter and the target is relative to the root of the drive.

This brings up a few questions

  1. Is this behavior a limitation of os.path.join? In other words, should os.path.join('d:\foo', '\bar') resolve to 'd:\bar'? As a Windows user, I tend to think so, but I hate to think that a mature function like path.join would need alteration to handle this use case.
  2. Is there an example of an existing target path resolver such as findpath that will work in all of these test cases?
  3. If 'no' to the above questions, how would you implement this desired behavior?

解决方案

I agree with you: this seems like a deficiency in os.path.join. Looks like you have to deal with the drives separately. This code passes all your tests:

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)))

(and yes, I had to nest two os.path.join's to get it to work...)

这篇关于在Windows中查找相对于另一个的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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