python 列出出当前目录及所有子目录下的文件

时间:2021-08-29 12:59:54
[root@web1 test]# cat walk.py 
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
for root, dirs, files in os.walk("/tmp", topdown=False):
    for name in files:
    print("文件")
        print(os.path.join(root,name))
    for name in dirs:
    print("目录")
        print(os.path.join(root,name))
[root@web1 test]# python walk.py 
目录
/tmp/systemd-private-93263caaf0954b00ac81e5145fa71ccc-chronyd.service-SmdVeb/tmp
文件
/tmp/1.txt
目录
/tmp/.X11-unix
目录
/tmp/.ICE-unix
目录
/tmp/.font-unix
目录
/tmp/.XIM-unix
目录
/tmp/.Test-unix
目录
/tmp/systemd-private-93263caaf0954b00ac81e5145fa71ccc-chronyd.service-SmdVeb