1

I am new to Python and I am trying to start CNN for one project. I mounted the gdrive and I am trying to download images from the gdrive directory. After, I am trying to count the images that I have in that directory. Here is my code:

import pathlib
dataset_dir = "content/drive/My Drive/Species_Samples"
data_dir = tf.keras.utils.get_file('Species_Samples', origin=dataset_dir, untar=True)
data_dir = pathlib.Path(data_dir)

image_count = len(list(data_dir('*/*.png')))
print(image_count)

However, I get the following error.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-78-e5d9409807d9> in <module>()
----> 1 image_count = len(list(data_dir('*/*.png')))
      2 print(image_count)

TypeError: 'PosixPath' object is not callable

Can you help, please?

After suggestion, my code looks like this:

import pathlib
data_dir = pathlib.Path("content/drive/My Drive/Species_Samples/")
count = len(list(data_dir.rglob("*.png")))
print(count)
Dpronin
  • 15
  • 3

1 Answers1

0

You are trying to glob files you need to use one of the glob methods that pathlib has:

import pathlib

data_dir = pathlib.Path("/path/to/dir/")

count = len(list(data_dir.rglob("*.png")))

In this case .rglob is a recursive glob.

Alex
  • 6,610
  • 3
  • 20
  • 38
  • Thanks for the suggestion. I appreciate it. However, it still prints count as 0. It sees the contents of main directory and contents of folders within the directory (PNG files) but still prints count as 0. Do you happen to know what could be the issue? – Dpronin Dec 03 '20 at 12:37
  • Are you sure that there are `.png` files in the directory? The text in the `rglob` method is case sensitive, so `*.png` ≠ `*.PNG`. – Alex Dec 03 '20 at 16:09
  • 1
    It looks like I missed slash before content in data_dir = pathlib.Path("content/drive/My Drive/Species_Samples/"). The code counts now. Thank you for your help. I appreciate it. – Dpronin Dec 05 '20 at 08:37