V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ray1888
V2EX  ›  Python

Python 的判断目录层级方法问题

  •  
  •   ray1888 ·
    ray1888 · 2017-06-12 10:49:28 +08:00 · 2965 次点击
    这是一个创建于 2517 天前的主题,其中的信息可能已经有所发展或是发生改变。
    假设有两个目录 A、B,Python 有模块可以直接判断出 A 是否是 B 的子目录吗?如果没有,只能用正则去进行匹配 dirname 吗?求大神解答并且提供好的方法
    14 条回复    2017-06-12 21:48:23 +08:00
    CoX
        1
    CoX  
       2017-06-12 11:02:37 +08:00
    os.path.dirname(B) == A
    不用正则也可以
    0asis
        2
    0asis  
       2017-06-12 11:07:40 +08:00
    A.startswith(B) ?
    imn1
        3
    imn1  
       2017-06-12 11:08:40 +08:00
    @CoX
    == 这个只能判断父级

    AB 均为 fullpath 的话,A in B 吧
    aa825aa
        4
    aa825aa  
       2017-06-12 11:30:50 +08:00
    import glob; if glob.glob("B/A"):return True
    binjjam
        5
    binjjam  
       2017-06-12 12:46:57 +08:00
    @imn1 你这个显然是错的老铁,
    C: /a/
    B: /a/b/
    A: /a/b/c

    感觉
    os.path.abspath(A).startswith(os.path.abspath(B))
    lll9p
        6
    lll9p  
       2017-06-12 13:02:17 +08:00   ❤️ 1
    https://gist.github.com/lll9p/4c2a352bda7245236467b19e527616dc

    用 pathlib 很方便把。py3.4 以上有。。
    lll9p
        7
    lll9p  
       2017-06-12 13:04:13 +08:00
    py3.4 以下的可以装 pathlib2
    imn1
        8
    imn1  
       2017-06-12 13:08:09 +08:00
    @binjjam
    受 1L 影响搞错方向

    In [6]: c="/a/"

    In [7]: b="/a/b/"

    In [8]: a="/a/b/c"

    In [9]: d="/a/c"

    In [10]: c in a
    Out[10]: True

    In [11]: d in a
    Out[11]: False

    In [12]: b in a
    Out[12]: True
    binjjam
        9
    binjjam  
       2017-06-12 14:14:41 +08:00   ❤️ 1
    @imn1
    B: /b/a/
    A: /a/
    A in B,但是
    imn1
        10
    imn1  
       2017-06-12 14:22:42 +08:00
    @binjjam
    U R right
    imn1
        11
    imn1  
       2017-06-12 14:27:13 +08:00
    顺便提醒 LZ,如果涉及写操作,还要考虑软硬连接
    ray1888
        12
    ray1888  
    OP
       2017-06-12 14:54:07 +08:00
    @imn1 现在还是在 windows 平台上测试,linux 上测试时候会考虑的
    hl
        13
    hl  
       2017-06-12 15:19:01 +08:00
    判断 A 是否是 B 的子目录
    换句话说,如果路径 B/A 存在,则 A 是 B 的子目录

    import os

    IS_SUB_DIR = os.path.isdir(os.path.join('B','A'))

    if IS_SUB_DIR: print("A is the subdir of B")
    hasdream
        14
    hasdream  
       2017-06-12 21:48:23 +08:00
    A = '/usr/local/abc'
    B = '/usr/local'
    B == A[:len(B)]
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1587 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:26 · PVG 00:26 · LAX 09:26 · JFK 12:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.