python中路径的跨平台拆分

python中路径的跨平台拆分,第1张

python中路径的跨平台拆分

OP指定“也将适用于Windows路径”。Windows路径有一些折痕。

首先,Windows有多个驱动器的概念,每一个都有自己当前的工作目录,

'c:foo'
'c:\foo'
往往是不一样的。因此,最好先使用os.path.splitdrive()分离出任何驱动器指示符。然后可以通过以下方法正确地重新组装路径(如果需要)
drive + os.path.join(*other_pieces)

其次,Windows路径可以包含斜杠或反斜杠或混合使用。因此,

os.sep
在解析非规范化路径时使用是没有用的。

更普遍:

产生的结果

'foo'
'foo/'
不应相同。

最好以“ os.path.split()将其输入视为不可拆分”来表示循环终止条件。

这是一个建议的解决方案,带有测试,包括与@Spacedman解决方案的比较

import os.pathdef os_path_split_asunder(path, debug=False):    parts = []    while True:        newpath, tail = os.path.split(path)        if debug: print repr(path), (newpath, tail)        if newpath == path: assert not tail if path: parts.append(path) break        parts.append(tail)        path = newpath    parts.reverse()    return partsdef spacedman_parts(path):    components = []     while True:        (path,tail) = os.path.split(path)        if not tail: return components        components.insert(0,tail)if __name__ == "__main__":    tests = [        '',        'foo',        'foo/',        'foo\',        '/foo',        '\foo',        'foo/bar',        '/',        'c:',        'c:/',        'c:foo',        'c:/foo',        'c:/users/john/foo.txt',        '/users/john/foo.txt',        'foo/bar/baz/loop',        'foo/bar/baz/',        '//hostname/foo/bar.txt',        ]    for i, test in enumerate(tests):        print "nTest %d: %r" % (i, test)        drive, path = os.path.splitdrive(test)        print 'drive, path', repr(drive), repr(path)        a = os_path_split_asunder(path)        b = spacedman_parts(path)        print "a ... %r" % a        print "b ... %r" % b        print a == b

这是输出(Python 2.7.1,Windows 7 Pro):

Test 0: ''drive, path '' ''a ... []b ... []TrueTest 1: 'foo'drive, path '' 'foo'a ... ['foo']b ... ['foo']TrueTest 2: 'foo/'drive, path '' 'foo/'a ... ['foo', '']b ... []FalseTest 3: 'foo\'drive, path '' 'foo\'a ... ['foo', '']b ... []FalseTest 4: '/foo'drive, path '' '/foo'a ... ['/', 'foo']b ... ['foo']FalseTest 5: '\foo'drive, path '' '\foo'a ... ['\', 'foo']b ... ['foo']FalseTest 6: 'foo/bar'drive, path '' 'foo/bar'a ... ['foo', 'bar']b ... ['foo', 'bar']TrueTest 7: '/'drive, path '' '/'a ... ['/']b ... []FalseTest 8: 'c:'drive, path 'c:' ''a ... []b ... []TrueTest 9: 'c:/'drive, path 'c:' '/'a ... ['/']b ... []FalseTest 10: 'c:foo'drive, path 'c:' 'foo'a ... ['foo']b ... ['foo']TrueTest 11: 'c:/foo'drive, path 'c:' '/foo'a ... ['/', 'foo']b ... ['foo']FalseTest 12: 'c:/users/john/foo.txt'drive, path 'c:' '/users/john/foo.txt'a ... ['/', 'users', 'john', 'foo.txt']b ... ['users', 'john', 'foo.txt']FalseTest 13: '/users/john/foo.txt'drive, path '' '/users/john/foo.txt'a ... ['/', 'users', 'john', 'foo.txt']b ... ['users', 'john', 'foo.txt']FalseTest 14: 'foo/bar/baz/loop'drive, path '' 'foo/bar/baz/loop'a ... ['foo', 'bar', 'baz', 'loop']b ... ['foo', 'bar', 'baz', 'loop']TrueTest 15: 'foo/bar/baz/'drive, path '' 'foo/bar/baz/'a ... ['foo', 'bar', 'baz', '']b ... []FalseTest 16: '//hostname/foo/bar.txt'drive, path '' '//hostname/foo/bar.txt'a ... ['//', 'hostname', 'foo', 'bar.txt']b ... ['hostname', 'foo', 'bar.txt']False


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5647097.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存