【Python】获取目录下所有文件,但不包括部分文件或子目录下的文件

dir_path = "/dir/path/"
ignore_dir_names = ["sub_dir1", "sub_dir2"]
ignore_file_names = ["file1", "file2", "file3"]

for root, dirs, files in os.walk(dir_path):
    # 排除特定目录
    if os.path.basename(root) in ignore_dir_names:
        ignore_dir_names += dirs
        continue
    for file in files:
        # 排除特定文件
        if file in ignore_file_names:
            continue

        full_path = os.path.join(root, file)
        print(full_path)

你可能感兴趣的:(python)