Home > Backend Development > Python Tutorial > 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
Release: 2023-04-19 12:37:03
forward
2192 people have browsed it

(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
Copy after login

(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)
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template