2022年 11月 5日

python 目录遍历

python目录遍历

  • 示例
import os


def get_files(filepath):
    for root, dirs, files in os.walk(filepath):
        # print("1:",root) #当前主目录
        # print("2:",dirs) #当前主目录下的所有目录
        # print("3:",files)  #当前主目录下的所有文件
        for file1 in files:
            print(os.path.join(root, file1))
        for dir1 in dirs:
            get_files(filepath+dir1)


def main():
    get_files('.')


if __name__ == '__main__':
    main()


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22