python中os.walk浏览目录和文件

#!/usr/bin/env python
# 2.py
# use UTF-8
# Python 3.3.0

# os.walk()的使用
import os

# 枚举dirPath目录下的所有文件

def main():
#begin
    fileDir = "F:" + os.sep + "aaa"        # 查找F:aaa 目录下    
    for root, dirs, files in os.walk(fileDir):
    #begin
        print(root)
        print(dirs)
        print(files)
    #end
    os.system("pause")
#end

if __name__ == '__main__':
#begin
    main()
#end


# 输出
# F:aaa
# ['4']
# ['1.txt', '2.txt', '3.txt']
# F:aaa4
# []
# ['5.txt', '6.txt', '7.txt']
原文地址:https://www.cnblogs.com/heshangaichirou/p/5455465.html