Lesson 2 // Why do we put path/o in brackets dest = (path/o)?

In the notebook there is a part of code that downloads the images of three types of bears, but in the code I don’t understand why do we put path/o in brackets?

Here is the code:

if not path.exists():
    path.mkdir() 
    for o in bear_types:
        dest = (path/o) #This one
        dest.mkdir(exist_ok=True)
        results = search_images_bing(key, f'{o} bear')
        download_images(dest, urls=results.attrgot('contentUrl'))
1 Like

We use brackets so that the next piece of code works on the whole path

assuming our list has the following bears

['black', 'teddy']

On the first loop our dest would be

dest = (path/'black')

Therefore our next code would be equal to

(path/'black').mkdir(exist_ok=True)

Meaning the mkdir function runs path as a whole

Without the brackets

path/'black'.mkdir(exist_ok=True)

Would result in a Syntax Error

Well, I tried and it wouldn’t, so that’s why it is confusing me. :smiley:

1 Like

Most probably the Path objet overloads the / 3. Data model — Python 3.3.7 documentation.truediv

1 Like

I edited my post. Tell me if it helps

1 Like