os模块打印出目录下所有文件路径

利用os模块,打印出当前路径下的所有文件名,使用到了递归函数。

import os

    def print_dir_constents(sPath):
        #列出当前路径下的所有文件夹和文件 并进行遍历
        for schild in os.listdir(sPath):
            #拼接地址
            sChildPath = os.path.join(sPath, schild)
            #判断当前遍历到的是文件还是文件夹
            if os.path.isdir(sChildPath):
                #再次递归调用
                print_dir_constents(sChildPath)
            else:
                print(sChildPath)


    print_dir_constents("/home/itcast/Desktop/Program")

猜你喜欢

转载自blog.csdn.net/weixin_40612082/article/details/81454315