Create directory only after download is ready

Avoid creating junky empty dirs on failed downloads
This commit is contained in:
Joshua Boniface 2023-04-03 04:52:23 -04:00
parent a9f54b8f42
commit 2d82329e4f
1 changed files with 4 additions and 3 deletions

7
c3dbdl
View File

@ -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)