Commit f8792d09 by Peter Weidenbach

get_dir_of_file_function added

parent 5c99beab
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, create_symlink, get_dir_of_file
from .file_functions import read_in_chunks, get_directory_for_filename, create_dir_for_file, human_readable_file_size 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 .git_functions import get_version_string_from_git
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, create_symlink
__version__ = '0.1.8'
__version__ = '0.1.9'
__all__ = [ __all__ = [
'get_directory_for_filename', 'get_directory_for_filename',
...@@ -18,4 +19,5 @@ __all__ = [ ...@@ -18,4 +19,5 @@ __all__ = [
'create_symlink', 'create_symlink',
'get_files_in_dir', 'get_files_in_dir',
'get_dirs_in_dir', 'get_dirs_in_dir',
'get_dir_of_file'
] ]
...@@ -183,3 +183,19 @@ def get_dirs_in_dir(directory_path): ...@@ -183,3 +183,19 @@ def get_dirs_in_dir(directory_path):
except Exception as e: except Exception as e:
logging.error("Could not get directories: {} {}".format(sys.exc_info()[0].__name__, e)) logging.error("Could not get directories: {} {}".format(sys.exc_info()[0].__name__, e))
return result return result
def get_dir_of_file(file_path):
"""
Returns absolute path of the directory including file
:param file_path: Paht of the file
:type: paht-like object
:return: string
"""
try:
return os.path.dirname(os.path.abspath(file_path))
except Exception as e:
logging.error("Could not get directory path: {} {}".format(sys.exc_info()[0].__name__, e))
return "/"
...@@ -6,7 +6,7 @@ from common_helper_files import get_binary_from_file, \ ...@@ -6,7 +6,7 @@ from common_helper_files import get_binary_from_file, \
write_binary_to_file, delete_file, get_safe_name, \ write_binary_to_file, delete_file, get_safe_name, \
get_files_in_dir, get_string_list_from_file, \ get_files_in_dir, get_string_list_from_file, \
get_dirs_in_dir, get_directory_for_filename, \ get_dirs_in_dir, get_directory_for_filename, \
create_symlink create_symlink, get_dir_of_file
from common_helper_files.fail_safe_file_operations import _get_counted_file_path from common_helper_files.fail_safe_file_operations import _get_counted_file_path
...@@ -107,6 +107,17 @@ class Test_FailSafeFileOperations(unittest.TestCase): ...@@ -107,6 +107,17 @@ class Test_FailSafeFileOperations(unittest.TestCase):
result = get_dirs_in_dir("/none_existing/dir") result = get_dirs_in_dir("/none_existing/dir")
self.assertEqual(result, [], "error result should be an empty list") self.assertEqual(result, [], "error result should be an empty list")
def test_get_dir_of_file_relative_path(self):
relative_path_result = get_dir_of_file("test/some_file")
expected_result = os.path.join(os.getcwd(), "test")
self.assertEqual(relative_path_result, expected_result)
def test_get_dir_of_file_absolute_path(self):
test_file_path = os.path.join(self.tmp_dir.name, 'test_file')
write_binary_to_file('test', test_file_path)
absolute_file_path_result = get_dir_of_file(test_file_path)
self.assertEqual(absolute_file_path_result, self.tmp_dir.name)
if __name__ == "__main__": if __name__ == "__main__":
unittest.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