A
A
Alexander Kovalenko2021-07-09 03:01:07
Python
Alexander Kovalenko, 2021-07-09 03:01:07

How to delete file in python that is downloaded?

import requests
import uuid
import os

unique_filename = str(uuid.uuid1())
path = 'tmp_user/' + unique_filename + '.jpg'


def gen():
    url = "https://thispersondoesnotexist.com/image"
    response = requests.get(url)
    if response.status_code == 200:
        with open(path, 'wb') as f:
            f.write(response.content)

input()
os.delete(path)

It is necessary that before the start of the input the picture is downloaded, and after pressing any button, it deletes the created file, but it turns out that the picture is not downloaded until the file ends

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ronald McDonald, 2021-07-09
@KovalenkoA12

Throw input and os.delete into the gen function.
And don't forget to call her.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question