Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

python - How to resolve an error 'list' object has no attribute 'iterdi'

I am working on an image dataset. When I am writing for loop I find the following error. That is

AttributeError: 'list' object has no attribute 'iterdi'

in line

for class_folder_path in DATA_FOLDER.iterdir():

My code:

 max_print_subfolders = 4
max_print_files_per_folder = 3
subfolder_counter = 0
for class_folder_path in DATA_FOLDER.iterdir():
    subfolder_counter += 1
    if subfolder_counter > max_print_subfolders:
        print(str('/content/DATA_FOLDER') + "more subfolders in this folder...")
        break
    file_counter = 0
    for image_path in class_folder_path.glob("*.png"):
        file_counter += 1
        if file_counter > max_print_files_per_folder:
            print(str(class_folder_path / '...') + "more files in this folder...
")
            break
        print(image_path)

Error: AttributeError: 'list' object has no attribute 'iterdi'

question from:https://stackoverflow.com/questions/65945445/how-to-resolve-an-error-list-object-has-no-attribute-iterdi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What is DATA_FOLDER? that should be a path (to a directory content) which you expect your directories are in it.

The iterdir() yields path objects of the directory contents.

from pathlib import Path

path = Path('D:/')
for _dir in path.iterdir():
    print(_dir)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...