Python下獲取當前目錄中的所有子目錄

p是輸入目錄

代碼如下

import os
def getDirList(p):  # 
    b = [];
    filepath=p
    if filepath=="":
        return b
    filepath = filepath.replace( "/","\\")
    if filepath[ -1] != "\\":
        filepath = filepath+"\\"
    a = os.listdir(filepath)
    #print(a)
    for x in a:
        path = filepath+x
        print(path)
        if os.path.isdir(path):
            c = getDirList(path);
            if c==[]:
                b.append(path)          
            else:
                print(c)
                b=b+c
    return b

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章