Skip to content

Commit

Permalink
Refactored logic to delete directory if empty
Browse files Browse the repository at this point in the history
  • Loading branch information
ozzieperez committed Dec 18, 2017
1 parent 2ce935f commit bc23eae
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ def download_to_file(filepath, url, session, headers, prefix_url=True):
print("Error downloading: " + filepath)
print(e)

# get the directory path
directory = os.path.dirname(filepath)
# if a file was being downloaded, delete it
if os.path.exists(filepath):
os.remove(filepath)

# delete directory if it's empty
directory = os.path.dirname(filepath)
if not os.listdir(directory):
os.rmdir(directory)
# if not empty, remove the current file being downloaded
elif os.path.exists(filepath):
os.remove(filepath)

# terminate program
sys.exit(1)
Expand Down

0 comments on commit bc23eae

Please sign in to comment.