diff options
author | Prabhu Ramachandran | 2016-10-21 23:12:41 +0530 |
---|---|---|
committer | GitHub | 2016-10-21 23:12:41 +0530 |
commit | 08d8478a6a57991b0130c5610db6efa7bc9e5a66 (patch) | |
tree | c94df7d571b2294a10833fe719ea51411db7e077 /yaksh/file_utils.py | |
parent | adae38cb47d8c36280f2e6cd40ccade3ce1108c5 (diff) | |
parent | 8e06cee7b1fea13d121d2d0677b0a8e3ea7f2e38 (diff) | |
download | online_test-08d8478a6a57991b0130c5610db6efa7bc9e5a66.tar.gz online_test-08d8478a6a57991b0130c5610db6efa7bc9e5a66.tar.bz2 online_test-08d8478a6a57991b0130c5610db6efa7bc9e5a66.zip |
Merge pull request #148 from adityacp/fix_extract_files
Fix Questions zip extract files
Diffstat (limited to 'yaksh/file_utils.py')
-rw-r--r-- | yaksh/file_utils.py | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index afcf9e8..f41c531 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -1,6 +1,7 @@ import shutil import os import zipfile +import tempfile def copy_files(file_paths): @@ -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, os.getcwd()) 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 |