Python 的判断目录层级方法问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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

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

  •  
  •   ray1888
    ray1888 Jun 12, 2017 3661 views
    This topic created in 3243 days ago, the information mentioned may be changed or developed.
    假设有两个目录 A、B,Python 有模块可以直接判断出 A 是否是 B 的子目录吗?如果没有,只能用正则去进行匹配 dirname 吗?求大神解答并且提供好的方法
    14 replies    2017-06-12 21:48:23 +08:00
    CoX
        1
    CoX  
       Jun 12, 2017
    os.path.dirname(B) == A
    不用正则也可以
    0asis
        2
    0asis  
       Jun 12, 2017
    A.startswith(B) ?
    imn1
        3
    imn1  
       Jun 12, 2017
    @CoX
    == 这个只能判断父级

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

    感觉
    os.path.abspath(A).startswith(os.path.abspath(B))
    lll9p
        6
    lll9p  
       Jun 12, 2017   1
    https://gist.github.com/lll9p/4c2a352bda7245236467b19e527616dc

    用 pathlib 很方便把。py3.4 以上有。。
    lll9p
        7
    lll9p  
       Jun 12, 2017
    py3.4 以下的可以装 pathlib2
    imn1
        8
    imn1  
       Jun 12, 2017
    @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  
       Jun 12, 2017   1
    @imn1
    B: /b/a/
    A: /a/
    A in B,但是
    imn1
        10
    imn1  
       Jun 12, 2017
    @binjjam
    U R right
    imn1
        11
    imn1  
       Jun 12, 2017
    顺便提醒 LZ,如果涉及写操作,还要考虑软硬连接
    ray1888
        12
    ray1888  
    OP
       Jun 12, 2017
    @imn1 现在还是在 windows 平台上测试,linux 上测试时候会考虑的
    hl
        13
    hl  
       Jun 12, 2017
    判断 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")
    zerofiny
        14
    zerofiny  
       Jun 12, 2017
    A = '/usr/local/abc'
    B = '/usr/local'
    B == A[:len(B)]
    About     Help     Advertise     Blog     API     FAQ     Solana     3660 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 45ms UTC 10:36 PVG 18:36 LAX 03:36 JFK 06:36
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86