'Unable to download image as .png/.jpg using PRAW and requests

I'm using Python to download memes from the /r/memes subreddit. Here's my code:

import praw
import requests

reddit = praw.Reddit(client_id="",
                     client_secret="",
                     user_agent="",
                     username="",
                     password="")

for submission in reddit.subreddit("memes").stream.submissions(skip_existing=True):
    print(submission.url)
    
    response = requests.get(submission.url)

    file = open(submission.id, "wb")  # line 15
    file.write(response.content)
    file.close()

My problem comes in at line 15. I'm able to download the image, but can't figure out how to download it as a .png/.jpg. Is there a way I can do this?



Solution 1:[1]

just documenting @jarhill0's response, you would write the image to a file like this:

extension = submission.url.rsplit('.')[-1]
with open(f"{submission.id}.{extension}", "wb") as file:
    file.write(response.content)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew Ryan