Need help in Understanding Code Logic

Hi,

What’s the logic behind the if condition in the below code?

2021-11-07_11-36

My understanding :

  • Line 5 : path.exists() - Returns True
  • Line 5 : not path.exists() - Returns False
  • Line 5 : if not path.exists() - Isn’t it the same as if False ??
    So, when if not path.exists() is False - How would the next line of code path.mkdir() execute??

Also, can we use the code path.mkdir(exist_ok=True) instead of
if not path.exists(): path.mkdir()
What is the difference between the 2 codes?

  • Line 11 : results=search_images_ddg(...) - In this code - we are not specifying the path to the individual directories of the 3 Categories of bears (grizzly,black,teddy). Then how are the images fetched & stored in the respective folders??

Can anyone please help me understand all the above??

Another issue with the above code :

This code is not working most of the time i.e. It is returning 0 images most of the times which can be confirmed using the below code :

fns = get_image_files(path)
fns
fns returns this : (#0) []

Only once did the code work fine returning 282 images.

This behavior is seen when I am trying the code in the Jarvis Labs console.

However, when I try the same code in Jupyter Lab locally its working fine & returning ample images.

Could anyone please guide me the reason behind the variation in results of the same code in different scenarios!!

Thanx in advance!!

line 5 explanation

# path.exists() returns true if path exists, else false, let us consider path didn't exists returns false, if not False: becomes if True, so dir will be created.
if not path.exists(): 
   path.mkdir()

Q: Also, can we use the code path.mkdir(exist_ok=True) instead of if not path.exists(): path.mkdir() What is the difference between the 2 codes?

  • you can use either of those methods, both creates directory if it didn’t exist

Q: Line 11 : results=search_images_ddg(…) - In this code - we are not specifying the path to the individual directories of the 3 Categories of bears (grizzly,black,teddy). Then how are the images fetched & stored in the respective folders??

  • search_images_ddg(key) function only returns list of urls, afterwards we need to call download_images(destination_path, urls=results.attrgot('contentUrl') , to download images, so only get_image_files(path) returns empty list.
  • maybe in your local environment you have already downloaded images so only you are getting 300 as result.

Hi Siva,

Thank you so much for your help!

Thanx for pointing out to the download_images logic.
My code is working fine now & the final result has ample images.

2 Issues!!

Issue 1 :

I understood the if condition logic.

But when below is the case
path = Path('bears') - Path created i.e. path.exists()=True
then,
if not path.exists(): - This condition becomes False
path.mkdir() - And, this code shouldn’t execute! Right!!

Is it so??

But, inspite of the fact that if not path.exists() is False - the next line path.mkdir() is still executing fine without throwing any error!!

How is it so??

Issue 2 :

Case 1:
Line 1 : path = Path('bears')
Line 2 : path.mkdir(exist_ok=True)

Case 2:
Line 1 : dest = (path/x)
Line 2 : dest.mkdir(exist_ok=True)

What’s the difference between Line1 & Line 2 in both the above cases??
What does each line do??

My code is running absolutely fine without throwing any errors & am getting ample images at the end - Even if I do not mention the 2nd Line.

Please see the below image where Line 2 mentioned in both the above 2 cases, is Commented out (Line 2 & Line 8) in the code - But still the code runs fine & gives the desired result.

I am getting the same results irrespective of mentioning those lines or commenting them i.e. Line 2 & Line 8 in the above image.

Can you please explain.

Thanx again for all the help!!

@Vnay0007 , Please check this github gist pathlib_path.ipynb · GitHub for better understanding.

@sirakr Thanx a lot!! Truly appreciate the effort. :pray: