0

The code i was trying to run is:

import numpy as np
import os
import cv2
import matplotlib.pyplot as plt
from os import listdir

from pathlib import Path

all_images = list(Path(r'D:/face/train').glob('**/*.jpg'))
np.array([np.array(cv2.imread(str(file))).flatten() for file in all_images])
Path = r'D:\face\train'
print(all_images[0])

the output is: D:\face\train\F0002\MID1\P00009_face3.jpg

train_images = ([x for x in all_images if val_families not in x])
val_images = ([x for x in all_images if val_families in x])

i'm getting the following error.

TypeError                                 Traceback (most recent call last)
<ipython-input-8-8de97a2e12c1> in <module>
----> 1 train_images = ([x for x in all_images if val_families not in x])
  2 val_images = ([x for x in all_images if val_families in x])

<ipython-input-8-8de97a2e12c1> in <listcomp>(.0)
----> 1 train_images = ([x for x in all_images if val_families not in x])
  2 val_images = ([x for x in all_images if val_families in x])

TypeError: argument of type 'WindowsPath' is not iterable

first of all i didnt use the Path class instead i was using the following command

 all_images = glob(train_folders_path + "*/*/*.jpg")
 print(all_images[0])

but here i was getting an index error. later i imported Path and tried the first code which gave me the output i wanted. but i'm getting error as i mentioned in the next line. Please help me solve this.

1
  • Question has nothing to do with machine-learning or jupyter-notebook - kindly do not spam irrelevant tags (removed).
    – desertnaut
    Commented Oct 26, 2019 at 17:29

1 Answer 1

1

here i was getting an index error.

glob returns a generator, not a container. You can iterate over glob's results, but you can only do that once, and you can't subscript to obtain the initial result.

If you want to use the results multiple times, or pick out the first one with [0], then use list( ... ) as your first code example showed. This will iterate over the results and store them in a list container that you can re-use or index to your heart's content.

Alternatively you could use next( ... ) to access just the initial result, but that doesn't seem to be what you want here.

EDIT

what does this WindowsPath is not iterable mean?

The list obtained from glob has several elements, and each of those elements is a Path. You can't iterate over a Path, just like you can't iterate over an int. You're certainly free to iterate over a list of Paths, or over a list of ints.

You could turn a Path into a str and iterate over that, as in the example below, but that's not what you want to do. Typically you would want to open(path, 'r') and iterate over that, which would produce lines from a text file, one line at a time.

>>> from pathlib import Path
>>> 
>>> path = Path('foo')
>>> path
PosixPath('foo')
>>>
>>> for x in path:
...   print(x)
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'PosixPath' object is not iterable
>>> 
>>> for x in str(path):
...   print(x)
... 
f
o
o
>>> 
1
  • what does this WindowsPath is not iterable mean? Commented Oct 26, 2019 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.