def demo(address): ? ?for x in os.listdir(address): ? ? ? ?if os.path.isdir(x): ? ? ? ? ? ?if os.path.getsize(x): ? ? ? ? ? ? ? ?address=address+"\\"+x ? ? ? ? ? ? ? ?demo(address) ? ? ? ? ? ?# else: ? ? ? ? ? ?# ? ? break ? ? ? ?else: ? ? ? ? ? ?print ("文件名:",x," ?文件地址:",address+x)demo("e:\\liying\\python")結(jié)果文件名: inspectionProfiles ? 文件地址: e:\liying\python\.ideainspectionProfiles文件名: misc.xml ? 文件地址: e:\liying\python\.ideamisc.xml文件名: modules.xml ? 文件地址: e:\liying\python\.ideamodules.xml文件名: python.iml ? 文件地址: e:\liying\python\.ideapython.iml文件名: workspace.xml ? 文件地址: e:\liying\python\.ideaworkspace.xml文件名: 廖雪峰python ? 文件地址: e:\liying\python\.idea廖雪峰python文件名: 新建文件夾 ? 文件地址: e:\liying\python\.idea新建文件夾文件名: 新建文本文檔.txt ? 文件地址: e:\liying\python\.idea新建文本文檔.txt其中 ???廖雪峰python ?和 ?新建文件夾 下面都有子目錄,為什么不能遍歷出來,而且和 ?廖雪峰python 有一個(gè)同級(jí)的文件夾的名稱,沒打印出來。附圖:
文件打開子目錄并遍歷
慕粉2243144748
2018-03-17 23:52:44