-2

i'm trying to practice transfer learning myself.

I'm trying to count the number of each cat and dog files (each 12500 pictures for cat and dog with the total of 25000 pictures).

Here is my code.Code

And here is my path for the picture folderenter image description here.

I thought this was a simple code, but still couldn't figure out why i keep getting (0,0) in my coding (supposed to be (12500 cat files,12500 dog files)):(.

curtis sohn
  • 47
  • 1
  • 6
  • I believe you need another slash at the end of the `path`, after Transfer. – Rachayita Giri Oct 23 '19 at 19:42
  • 2
    Please paste your code here instead of providing an image of your code. You could use a markdown code block with ```python your code ```. – CypherX Oct 23 '19 at 19:42
  • **Note:** Also make sure that your files have a valid image format (jpg, png, etc). Currently it looks like their names are like `'cat.0'` (does that mean your image extension is '.0', or you have the file extension hidden?). – CypherX Oct 23 '19 at 20:01

1 Answers1

1

Use os.path.join() inside glob.glob(). Also, if all your images are of a particular extension (say, jpg), you could replace '*.*' with '*.jpg*' for example.

Solution

import os, glob

files = glob.glob(os.path.join(path,'train/*.*'))

As a matter of fact, you might as well just do the following using os library alone, since you are not selecting any particular file extension type.

import os
files = os.listdir(os.path.join(path,'train'))

Some Explanation

The method os.path.join() here helps you join multiple folders together to create a path. This will work whether you are on a Windows/Mac/Linux system. But, for windows the path-separator is \ and for Mac/Linux it is /. So, not using os.path.join() could create an un-resolvable path for the OS. I would use glob.glob when I am interested in getting some specific types (extensions) of files. But glob.glob(path) requires a valid path to work with. In my solution, os.path.join() is creating that path from the path components and feeding it into glob.glob().

For more clarity, I suggest you see documentation for os.path.join and glob.glob.

Also, see pathlib module for path manipulation as an alternative to os.path.join().

CypherX
  • 7,019
  • 3
  • 25
  • 37
  • Thank you! it worked! Could you tell me difference between glob.glob() and using os.path.join() inside glob.glob()?? – curtis sohn Oct 24 '19 at 06:50
  • @curtissohn Please consider **`accepting`** and **`upvoting`** the answer. Thank you. – CypherX Oct 24 '19 at 09:25
  • `os.path.join()` helps you join multiple folders together to create a path. This will work whether you are on a Windows/Mac/Linux system. But as you know, for windows the path-separator is **\** and for Mac/Linux it is **/**, not using `os.path.join()` could create an un-resolvable path for the OS. I would use `glob.glob` when I am interested in getting some specific types of files. But `glob.glob(path)` requires a valid path to work with. In my solution, `os.path.join() is creating that path from the path components and feeding it into `glob.glob()`. For more clarity, I suggest you see docs. – CypherX Oct 24 '19 at 09:31
  • @curtissohn Please **`accept`** the solution from the left end (there is a tick mark: when you click on that it would turn green). [How to **accept** an answer?](https://stackoverflow.com/help/someone-answers). – CypherX Oct 24 '19 at 21:15