From 2d82329e4ff1757bf07d81e1b6f2fff3fe777f24 Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Mon, 3 Apr 2023 04:52:23 -0400 Subject: [PATCH] Create directory only after download is ready Avoid creating junky empty dirs on failed downloads --- c3dbdl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/c3dbdl b/c3dbdl index b4901ff..9e03703 100755 --- a/c3dbdl +++ b/c3dbdl @@ -109,9 +109,6 @@ def downloadSong(destination, filename, entry): download_filename = f"{destination}/{download_filename}" download_path = '/'.join(f"{download_filename}".split('/')[0:-1]) - if not os.path.exists(download_path): - os.makedirs(download_path) - if os.path.exists(download_filename): click.echo(f"File exists at {download_filename}") return None @@ -135,6 +132,10 @@ def downloadSong(destination, filename, entry): else: code = "-1" raise HTTPError(download_url, code, "", None, None) + + if not os.path.exists(download_path): + os.makedirs(download_path) + with open(download_filename, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk)