From 21f53d77a83ce683ad64b2031cd2b8b7aba05c26 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 11 Jul 2016 16:43:35 +0530 Subject: changed module name from copy_delete_files to file_utils --- yaksh/file_utils.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 yaksh/file_utils.py (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py new file mode 100644 index 0000000..1e03f1f --- /dev/null +++ b/yaksh/file_utils.py @@ -0,0 +1,33 @@ +import shutil +import os +import zipfile + + +def copy_files(file_paths): + """ Copy Files to current directory, takes + tuple with file paths and extract status""" + + files = [] + for src in file_paths: + file_path, extract = src + file_name = os.path.basename(file_path) + files.append(file_name) + shutil.copy(file_path, os.getcwd()) + if extract: + unzip = zipfile.ZipFile(file_name) + for zip_files in unzip.namelist(): + files.append(zip_files) + unzip.extractall() + unzip.close() + return files + + +def delete_files(files): + """ Delete Files from current directory """ + + for content in files: + if os.path.exists(content): + if os.path.isfile(content): + os.remove(content) + else: + shutil.rmtree(content) -- cgit From 4c6b1eb85f82536bfdcdee9cc7170a4a51cf5bf4 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 28 Jul 2016 15:51:42 +0530 Subject: added validation to check zip file --- yaksh/file_utils.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 1e03f1f..8f6f6e5 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -13,7 +13,7 @@ def copy_files(file_paths): file_name = os.path.basename(file_path) files.append(file_name) shutil.copy(file_path, os.getcwd()) - if extract: + if extract and zipfile.is_zipfile(file_name): unzip = zipfile.ZipFile(file_name) for zip_files in unzip.namelist(): files.append(zip_files) @@ -25,9 +25,9 @@ def copy_files(file_paths): def delete_files(files): """ Delete Files from current directory """ - for content in files: - if os.path.exists(content): - if os.path.isfile(content): - os.remove(content) + for file in files: + if os.path.exists(file): + if os.path.isfile(file): + os.remove(file) else: - shutil.rmtree(content) + shutil.rmtree(file) -- cgit