Python 打印某一目录下所有文件名

import os

def printName(dir):
    list_dir = os.listdir(dir)
    for name in list_dir:
        if os.path.isdir(dir+'/'+name):
            printName(dir+'/'+name)
        elif os.path.isfile(dir+'/'+name):
            print name
        else:
            pass

if __name__ == '__main__':
    dir = os.getcwd()
    dir = '/'.join(dir.split('/')[:-1])
    printName(dir)

你可能感兴趣的:(Python)