python学习:递归列出目录里的文件

#!/usr/bin/python
 
import os
import sys
 
def print_files(path):
    lsdir = os.listdir(path)
    dirs = [i for i in lsdir if os.path.isdir(os.path.join(path,i))]
    files = [i for i in lsdir if os.path.isfile(os.path.join(path,i))]
    if files:
        for f in files:
            print os.path.join(path, f)
    if dirs:
        for d in dirs:
            print_files(os.path.join(path, d))
 
print_files(sys.argv[1])
 
执行:
python 8.py .
显示当前目录下文件
 
 
#!/usr/bin/python
 
import os
import sys
 
def print_files(path):
    lsdir = os.listdir(path)
    dirs = [i for i in lsdir if os.path.isdir(os.path.join(path,i))]
    files = [i for i in lsdir if os.path.isfile(os.path.join(path,i))]
    if dirs:
        for d in dirs:
            print_files(os.path.join(path, d))
    if files:
        for f in files:
            print os.path.join(path, f)
 
print_files(sys.argv[1])
 
执行时最深的文件先显示 
posted @ 2017-09-14 09:40  Oops!#  阅读(365)  评论(0编辑  收藏  举报