Home  >  Article  >  Backend Development  >  How to get all files in a specified directory using Python code set pathlib application

How to get all files in a specified directory using Python code set pathlib application

WBOY
WBOYforward
2023-04-19 12:37:032166browse

(1) The following code, by default, recursively obtains all files in the specified directory root_dir. When the recursive parameter is specified as False, only all files in the root_dir directory are obtained, and no recursive search is performed. If the suffix_tuple parameter is specified, then You can obtain the specified suffix file in the root_dir directory

from pathlib import Path

def get_all_files(root_dir,recursive=True,suffix_tuple=()):
    all_files=[]
    if Path(root_dir).exists():
        if Path(root_dir).is_dir():
            if recursive:
                for elem in Path(root_dir).glob("**/*"):
                    if Path(elem).is_file():
                        suffix=Path(elem).suffix
                        if not suffix_tuple:
                            all_files.append(elem)
                        else:
                            if suffix in suffix_tuple:
                                all_files.append(elem)
            else:
                for elem in Path(root_dir).iterdir():
                    if Path(elem).is_file():
                        suffix=Path(elem).suffix
                        if not suffix_tuple:
                            all_files.append(elem)
                        else:
                            if suffix in suffix_tuple:
                                all_files.append(elem)
        else:
            all_files.append(root_dir)
    return all_files

(2) The specific usage method is as follows, that is, the test code, the specific directory path is specified as the directory where it exists

if __name__=="__main__":
    path="D:/gitee/oepkgs/mugen/testcases/cli-test/acl/oe_test_acl_defaulr_rule.sh"
    for elem in get_all_files(path):
        print(elem)
    print("-------------------------------------------------")
    path = "D:/gitee/oepkgs/mugen/testcases/cli-test/acl"
    for elem in get_all_files(path):
        print(elem)
    print("-------------------------------------------------")
    path = "D:/gitee/oepkgs/mugen/testcases/cli-test/acl"
    for elem in get_all_files(path,False):
        print(elem)
    print("-------------------------------------------------")
    path = "D:/gitee/oepkgs/mugen/testcases/cli-test/acl"
    for elem in get_all_files(path, True,(".sh",)):
        print(elem)
    print("-------------------------------------------------")
    path = "D:/gitee/oepkgs/mugen/testcases/cli-test/acl"
    for elem in get_all_files(path, True, (".json",)):
        print(elem)

The execution results are as follows, first When the path is specified as a file, the file is directly returned as the query result. The last one specifies the .json suffix. Because there is no json file on the debugging machine, the print is empty

How to get all files in a specified directory using Python code set pathlib application

The above is the detailed content of How to get all files in a specified directory using Python code set pathlib application. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete