Commit cdea1cb8 by Peter Weidenbach

get list of lines from text file functionality added

parent 8720d5a7
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, 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
from .config_functions import update_config_from_env
__version__ = '0.1.5'
__version__ = '0.1.6'
__all__ = [
'get_directory_for_filename',
......@@ -14,6 +14,7 @@ __all__ = [
'get_version_string_from_git',
'md5sum',
'get_binary_from_file',
'get_string_list_from_file',
'write_binary_to_file',
'get_safe_name',
'delete_file',
......
......@@ -27,6 +27,24 @@ def get_binary_from_file(file_path):
return binary
def get_string_list_from_file(file_path):
"""
Fail-safe file read operation returning a list of text strings.
Errors are logged. No exception raised.
:param file_path: Path of the file. Can be absolute or relative to the current directory.
:type file_path: str
:return: file's content as text string list; returns empty list on error
"""
try:
raw = get_binary_from_file(file_path)
string = raw.decode(encoding='utf_8', errors='replace')
return string.split("\n")
except Exception as e:
logging.error("Could not read file: {} {}".format(sys.exc_info()[0].__name__, e))
return []
def write_binary_to_file(file_binary, file_path, overwrite=False, file_copy=False):
"""
Fail-safe file write operation. Creates directories if needed.
......
first line
second line
thÿrd line
first line
\ No newline at end of file
......@@ -4,7 +4,7 @@ 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_files_in_dir, get_string_list_from_file
from common_helper_files.file_functions import get_directory_for_filename
......@@ -29,6 +29,11 @@ class Test_FailSafeFileOperations(unittest.TestCase):
file_binary = get_binary_from_file(none_existing_file_path)
self.assertEqual(file_binary, b'', "content not correct")
def test_fail_safe_read_file_string_list(self):
test_file_path = os.path.join(self.get_directory_of_current_file(), "data", "multiline_test.txt")
lines = get_string_list_from_file(test_file_path)
self.assertEqual(lines, ['first line', 'second line', 'th\ufffdrd line', '', 'first line'], "lines not correct")
def test_fail_safe_write_file(self):
file_path = os.path.join(self.tmp_dir.name, "test_folder", "test_file")
write_binary_to_file(b'this is a test', file_path)
......@@ -76,7 +81,7 @@ class Test_FailSafeFileOperations(unittest.TestCase):
result = get_files_in_dir(test_dir_path)
self.assertIn(os.path.join(test_dir_path, "read_test"), result, "file in root folder not found")
self.assertIn(os.path.join(test_dir_path, "test_folder/generic_test_file"), result, "file in sub folder not found")
self.assertEqual(len(result), 2, "number of found files not correct")
self.assertEqual(len(result), 3, "number of found files not correct")
def test_get_files_in_dir_error(self):
result = get_files_in_dir("/none_existing/dir")
......
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