-1

I am getting the link of a gif from a site using bs4, it saves the url as a gif just fine since I can find the gif and open it from the file, however when I run the code it says it cant find the directory named "/tmp/fo.gif".

gif = soup.select_one("#gif.borders") or ""
    if gif:
        gif = str(soup.img["src"])
        with open('/tmp/fo.gif', 'wb') as f:
            f.write(requests.get(gif).content)
            gif = Image.open('/tmp/fo.gif')
            gif = (gif.n_frames)
baduker
  • 15,071
  • 9
  • 26
  • 44
  • Paths in python are relative to working directory, question answered here: https://stackoverflow.com/questions/72177485/open-a-file-in-python-from-2-directory-back/72177662#72177662 – annes May 09 '22 at 20:38
  • @annes -- Did you not notice he is using an absolute path? – Tim Roberts May 09 '22 at 20:39
  • 2
    The PROBLEM here is that you are trying to open the file inside the `with` clause, while you still have it open for writing. Until the file is closed, it does not exist. Un-indent those last two lines, and you should find happiness. – Tim Roberts May 09 '22 at 20:40
  • @TimRoberts that is my bad, missed that! – annes May 09 '22 at 20:42
  • @TimRoberts thanks very much! I found happiness with just 2 clicks :) and thanks annes for the answer! – idontusefolioscope May 09 '22 at 20:46

0 Answers0