From 1bfd71cc024dc7ea72099deff52efbeac7db87b5 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 17 Oct 2016 12:43:13 +0530 Subject: extract uploaded questions zip in temp --- yaksh/file_utils.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index afcf9e8..ba3ead0 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -1,7 +1,8 @@ import shutil import os import zipfile - +import tempfile +from online_test.settings import OUTPUT_DIR def copy_files(file_paths): """ Copy Files to current directory, takes @@ -14,16 +15,19 @@ def copy_files(file_paths): files.append(file_name) shutil.copy(file_path, os.getcwd()) if extract: - z_files = extract_files(file_name) + z_files, path = extract_files(file_name, OUTPUT_DIR) for file in z_files: files.append(file) return files -def delete_files(files): - """ Delete Files from current directory """ - - for file in files: +def delete_files(files, file_path=None): + """ Delete Files from directory """ + for file_name in files: + if file_path: + file = os.path.join(file_path, file_name) + else: + file = file_name if os.path.exists(file): if os.path.isfile(file): os.remove(file) @@ -31,13 +35,18 @@ def delete_files(files): shutil.rmtree(file) -def extract_files(zip_file): +def extract_files(zip_file, path=None): + """ extract files from zip """ zfiles = [] if zipfile.is_zipfile(zip_file): zip_file = zipfile.ZipFile(zip_file, 'r') for z_file in zip_file.namelist(): zfiles.append(z_file) - zip_file.extractall() + if path: + extract_path = path + else: + extract_path = tempfile.gettempdir() + zip_file.extractall(extract_path) zip_file.close() - return zfiles + return zfiles, extract_path -- cgit