diff options
author | adityacp | 2016-10-17 12:43:13 +0530 |
---|---|---|
committer | adityacp | 2016-10-17 12:44:16 +0530 |
commit | 1bfd71cc024dc7ea72099deff52efbeac7db87b5 (patch) | |
tree | 8ed81eea85902155fed1b29f7c8256b5d0d2e187 /yaksh/file_utils.py | |
parent | 9b60cc7b8f000f96d5f818f759a6c63d5a26f239 (diff) | |
download | online_test-1bfd71cc024dc7ea72099deff52efbeac7db87b5.tar.gz online_test-1bfd71cc024dc7ea72099deff52efbeac7db87b5.tar.bz2 online_test-1bfd71cc024dc7ea72099deff52efbeac7db87b5.zip |
extract uploaded questions zip in temp
Diffstat (limited to 'yaksh/file_utils.py')
-rw-r--r-- | yaksh/file_utils.py | 27 |
1 files changed, 18 insertions, 9 deletions
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 |