Python中的os.walk
阿新 • • 發佈:2018-12-29
舉個例子:檔案分佈如下,NULL表示空目錄---------------------------------------
A -> AA -> aa1.txt aa2.txt -> AB -> NULL -> AC -> NULL -> test.py------------------------------------------
程式碼1:>>> import os>>>>>> for root, dirs, files in os.walk(".", topdown=False):... for name in files:... print(os.path.join(root, name))... for name in dirs:... print(os.path.join(root, name))....\AA\aa1.txt.\AA\aa2.txt.\test.py.\AA.\AB.\AC--------------------------------------------
程式碼2:>>> for root, dirs, files in os.walk(".", topdown=True):... for name in files:... print(os.path.join(root, name))... for name in dirs:... print(os.path.join(root, name))....\test.py.\AA.\AB.\AC.\AA\aa1.txt.\AA\aa2.txt
A -> AA -> aa1.txt aa2.txt -> AB -> NULL -> AC -> NULL -> test.py------------------------------------------
程式碼1:>>> import os>>>>>> for root, dirs, files in os.walk(".", topdown=False):... for name in files:... print(os.path.join(root, name))... for name in dirs:... print(os.path.join(root, name))....\AA\aa1.txt.\AA\aa2.txt.\test.py.\AA.\AB.\AC--------------------------------------------
程式碼2:>>> for root, dirs, files in os.walk(".", topdown=True):... for name in files:... print(os.path.join(root, name))... for name in dirs:... print(os.path.join(root, name))....\test.py.\AA.\AB.\AC.\AA\aa1.txt.\AA\aa2.txt