首页 > 其他 > 详细

遍历目录

时间:2019-02-14 00:15:45      阅读:193      评论:0      收藏:0      [点我收藏+]
import os

root_path1 = rD:\python_code
file_count = 0
dir_count = 0


def list_files(root_path):
    """
    遍历目录
    :param root_path:
    :return:
    """
    global file_count, dir_count
    if os.path.isfile(root_path):
        print(root_path)
        file_count += 1
    else:
        res = os.listdir(root_path)
        for file in res:
            full_path = os.path.join(root_path, file)
            print(full_path)
            if os.path.isfile(full_path):
                print(full_path)
                file_count += 1
            else:
                dir_count += 1
                list_files(full_path)


def walk_files(root_path):
    """
    遍历目录
    :param root_path:
    :return:
    """
    global file_count, dir_count
    for root_dir, dirs, files in os.walk(root_path, topdown=True):
        for file in files:
            print(os.path.join(root_path, file))
            file_count += 1
        for dir1 in dirs:
            print(os.path.join(root_path, dir1))
            dir_count += 1


list_files(root_path1)
print(file_count)
print(dir_count)
print("----------------------")
walk_files(root_path1)
print(file_count)
print(dir_count)

 

遍历目录

原文:https://www.cnblogs.com/sunBinary/p/10372399.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!