Error trying to save retrieved image: "FileNotFoundError: [WinError3] The specified path could not be found."

Asked 1 years ago, Updated 1 years ago, 77 views

After using the Jupiter Notebook in Windows 10 and installing the module with the pip command after obtaining the Flicker API key, the following program results in an error:

How should I deal with it?Please let me know.

Running Programs

Search and download photos in #Flickr
from flickrapi import FlickrAPI
from urllib.request import urlretrieve
from print import print
importos, time, sys

# Specify API key and secret (★ Rewrite below ★) ---(*1)
key="41c9b3fc161f7cab49d92985af058831"
secret="0a0fdc4f2b74cb90"
wait_time = 1# Number of seconds to wait (recommended at least 1)

# Download with keyword and directory name ---(*2)
defmain():
    go_download('Tuna Sushi', 'sushi')
    go_download('salad','salad')
    go_download('Mabo Tofu', 'tofu')

# Search Flickr API for photos ---(*3)
def go_download(keyword,dir):
    # Determine image retention path
    savedir="./image/"+dir
    if notos.path.exists(savedir):
        os.mkdir (savedir)
    # Download using API ---(*4)
    flickr = FlickrAPI(key, secret, format = 'parsed-json')
    res = flickr.photos.search(
      text=keyword, #searchword
      per_page=300, #Number of acquisitions
      media='photos',# Search for photos
      sort = "release", # sort by search terms
      safe_search=1,#safesearch
      extras='url_q, license')
    # Review Search Results
    photos=res ['photos']
    print (photos)
    try:
      # Download images one at a time ---(*5)
      for i, photo in enumerate (photos['photo']):
        url_q = photo ['url_q' ]
        filepath=savedir+'/'+photo['id']+'.jpg'
        ifos.path.exists(filepath): continue
        print(str(i+1)+":download=",url_q)
        urlretrieve(url_q, filepath)
        time.sleep(wait_time)
    except:
      import traceback
      traceback.print_exc()

if__name__=='__main__':
    main()

Error encountered

FileNotFoundError Traceback (most recent call last)
<ipython-input-10-23621884de0c>in<module>
     48 
     49 if__name__=='__main__':
--- > 50 main()

<ipython-input-10-23621884de0c>in main()
     12# Download with keyword and directory name ---(*2)
     13 def main():
--->14 go_download ('Tuna Sushi', 'sushi')
     15 go_download('Salad','salad')
     16 go_download ('Mabo Tofu', 'tofu')

<ipython-input-10-23621884de0c> ingo_download (keyword, dir)
     21 savedir="./image/"+dir
     22 if notos.path.exists(savedir):
--- > 23os.mkdir (savedir)
     24# Download using API ---(*4)
     25 flickr = FlickrAPI (key, secret, format = 'parsed-json')

FileNotFoundError: [WinError3] The specified path could not be found: './image/sushi'

python python3 jupyter-notebook

2022-09-29 21:59

1 Answers

I think it was probably the situation in this article.
Makedirs

to recursively create deep hierarchical directories in Python

Error creating new directory in os.mkdir in a directory that does not exist
os.mkdir() used to create a directory (folder) in Python, but an error (FileNotFoundError) occurs when trying to create a new directory in a directory that does not exist.

importos

os.mkdir('not_exist_dir/new_dir')
# FileNotFoundError

In the context of the question, the following error occurred, so there was no image directory on the upper side.

FileNotFoundError: [WinError3] The specified path could not be found: './image/sushi'

The countermeasure for this is

Create a directory recursively in os.madeirs
Using os.madeirs() instead of os.mkdir() creates an intermediate directory, allowing you to recursively create a directory in a deep hierarchy.

os.madeirs('not_exist_dir/new_dir')

In this example, the intermediate directory not_exist_dir and the terminal directory new_dir are created at once.Multiple new intermediate directories are acceptable.

So I'm going to use this part of the question source.

 if notos.path.exists(savedir):
    os.mkdir (savedir)

The solution should be as follows:

 if notos.path.exists(savedir):
    os.madeirs (savedir)


2022-09-29 21:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.