From 23499945bb053381305d7c07a3e3498df9b15c4a Mon Sep 17 00:00:00 2001 From: Ishaan <45380942+ishaan-mehta@users.noreply.github.com> Date: Sun, 24 Oct 2021 01:23:42 -0500 Subject: [PATCH] Clean download_and_extract() function Get rid of unused variable "fn" Use "with" statement instead of "try"/"finally" to open tar file --- utils/utils.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/utils/utils.py b/utils/utils.py index 9acdea0..8861bcb 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -8,8 +8,7 @@ def download_and_extract(): url = "https://s3.us-west-2.amazonaws.com/dgl-data/dataset/DRKG/drkg.tar.gz" path = "../data/" filename = "drkg.tar.gz" - fn = os.path.join(path, filename) - if os.path.exists("../data/drkg/drkg.tsv"): + if os.path.exists(os.path.join(path, filename)): return opener, mode = tarfile.open, 'r:gz' @@ -18,9 +17,8 @@ def download_and_extract(): os.chdir(path) while True: try: - file = opener(filename, mode) - try: file.extractall() - finally: file.close() + with opener(filename, mode) as file: + file.extractall() break except Exception: f_remote = requests.get(url, stream=True)