From 7f12e083e87fcff1803c087e974a978ddf654a7f Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 27 Apr 2023 00:08:28 +0200 Subject: [PATCH] Make downloads atomic Should prevent corrupted/semi-downloaded files when interrupting the download script while it's writing a file. --- archive_imgur_images/download_images.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/archive_imgur_images/download_images.py b/archive_imgur_images/download_images.py index 5a435b1..4f3fe75 100755 --- a/archive_imgur_images/download_images.py +++ b/archive_imgur_images/download_images.py @@ -17,6 +17,7 @@ def log(ids, n, id, msg): def download(ids, n, id, path, link): + tmppath = path.parent / (path.name + ".tmp") try: r = requests.get(link) if r.status_code == 404: @@ -25,8 +26,9 @@ def download(ids, n, id, path, link): elif r.status_code != 200: log(ids, n, id, f"Weird status code: {r.status_code}") return - with open(path, "wb") as f: + with open(tmppath, "wb") as f: f.write(r.content) + tmppath.rename(path) log(ids, n, id, "Downloaded") except Exception as e: log(ids, n, id, f"Error fetching {link}: {e}")