search()小實例

編寫一個search(s)的函數,能在當前目錄以及當前目錄的所有子目錄下查找文件名包含指定字符串的文件,並打印出完整路徑:

def search(dir,w):
    for x in os.listdir(dir):
        nf=os.path.join(dir,x)
        if os.path.isfile(nf) and w in nf:
            print nf
        elif os.path.isdir(nf):
            search(nf,w)

search('.','test')


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