zoukankan      html  css  js  c++  java
  • 遍历目录

    import os
    
    root_path1 = r'D:\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)
  • 相关阅读:
    UIImageView变灰
    IOS 瀑布流
    IOS9适配 MARK
    MAC PHP MARK
    IOS第三方库 MARK
    IOS聊天对话界面
    UILabel自适应宽度的函数详解
    UIControl的使用
    IOS @2X.png
    自定义UIAlertView
  • 原文地址:https://www.cnblogs.com/sunBinary/p/10372399.html
Copyright © 2011-2022 走看看