From 86589580a23ed6f1f94c6c18bcb21870b2efca58 Mon Sep 17 00:00:00 2001 From: Igor Ryabchikov Date: Mon, 27 Sep 2021 00:28:57 +0300 Subject: [PATCH] Files download util fix --- common/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/common/__init__.py b/common/__init__.py index d934c95..58e2fbc 100644 --- a/common/__init__.py +++ b/common/__init__.py @@ -1,4 +1,6 @@ +import errno import logging +import os import requests @@ -7,6 +9,14 @@ logging_levels = {'CRITICAL': logging.CRITICAL, 'ERROR': logging.ERROR, 'WARNING def download_file(url, path): + if not os.path.exists(os.path.dirname(path)): + try: + os.makedirs(os.path.dirname(path)) + except OSError as exc: + # На случай параллельного создания директории + if exc.errno != errno.EEXIST: + raise + with requests.get(url, allow_redirects=True, stream=True) as r: r.raise_for_status() with open(path, 'wb') as f: -- GitLab