Finding All File Paths in a Directory

  • Share this:

Code introduction


This function traverses all files under the given path and returns a list containing the full paths of all files.


Technology Stack : os.walk

Code Type : Function

Code Difficulty : Intermediate


                
                    
import re
import os
import sys
import json

def find_files(root_path):
    """
    遍历给定路径下的所有文件,返回文件列表。
    
    :param root_path: str,表示根目录路径
    :return: list,包含所有文件的完整路径
    """
    files = []
    for dirpath, dirnames, filenames in os.walk(root_path):
        for filename in filenames:
            files.append(os.path.join(dirpath, filename))
    return files                
              
Tags: