Commit cdec6f04 by Peter Weidenbach

get_dirs_in_dir function added

parent 1be772aa
from .file_functions import read_in_chunks, get_directory_for_filename, create_dir_for_file, human_readable_file_size
from .git_functions import get_version_string_from_git
from .hash_functions import md5sum
from .fail_safe_file_operations import get_binary_from_file, get_string_list_from_file, write_binary_to_file, get_safe_name, delete_file, get_files_in_dir
from .fail_safe_file_operations import get_binary_from_file, get_string_list_from_file, write_binary_to_file, get_safe_name, delete_file, get_files_in_dir, get_dirs_in_dir
from .config_functions import update_config_from_env
__version__ = '0.1.6'
__version__ = '0.1.7'
__all__ = [
'get_directory_for_filename',
......@@ -19,5 +19,6 @@ __all__ = [
'get_safe_name',
'delete_file',
'get_files_in_dir',
'update_config_from_env',
'get_dirs_in_dir',
'update_config_from_env'
]
......@@ -143,3 +143,23 @@ def get_files_in_dir(directory_path):
except Exception as e:
logging.error("Could not get files: {} {}".format(sys.exc_info()[0].__name__, e))
return result
def get_dirs_in_dir(directory_path):
"""
Returns a list with the absolute paths of all 1st level sub-directories in the directory directory_path.
:param directory_path: directory including sub-directories
:type directory_path: str
:return: list
"""
result = []
try:
dir_content = os.listdir(directory_path)
for item in dir_content:
dir_path = os.path.join(directory_path, item)
if os.path.isdir(dir_path):
result.append(dir_path)
except Exception as e:
logging.error("Could not get directories: {} {}".format(sys.exc_info()[0].__name__, e))
return result
......@@ -2,10 +2,11 @@ import unittest
import os
from tempfile import TemporaryDirectory
from common_helper_files.fail_safe_file_operations import get_binary_from_file,\
write_binary_to_file, delete_file, get_safe_name, _get_counted_file_path,\
get_files_in_dir, get_string_list_from_file
from common_helper_files.file_functions import get_directory_for_filename
from common_helper_files import get_binary_from_file,\
write_binary_to_file, delete_file, get_safe_name,\
get_files_in_dir, get_string_list_from_file,\
get_dirs_in_dir, get_directory_for_filename
from common_helper_files.fail_safe_file_operations import _get_counted_file_path
class Test_FailSafeFileOperations(unittest.TestCase):
......@@ -87,5 +88,17 @@ class Test_FailSafeFileOperations(unittest.TestCase):
result = get_files_in_dir("/none_existing/dir")
self.assertEqual(result, [], "error result should be an empty list")
def test_get_dirs(self):
test_dirs = ["dir_1", "dir_2", "dir_1/sub_dir"]
for item in test_dirs:
os.mkdir(os.path.join(self.tmp_dir.name, item))
result = get_dirs_in_dir(self.tmp_dir.name)
self.assertEqual(sorted(result), [os.path.join(self.tmp_dir.name, "dir_1"), os.path.join(self.tmp_dir.name, "dir_2")], "found dirs not correct")
def test_get_dirs_in_dir_error(self):
result = get_dirs_in_dir("/none_existing/dir")
self.assertEqual(result, [], "error result should be an empty list")
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment