add read methods to text and json files
This commit is contained in:
57
src/utilities/textfile.py
Normal file
57
src/utilities/textfile.py
Normal file
@@ -0,0 +1,57 @@
|
||||
__author__ = 'stubbfel'
|
||||
import os.path
|
||||
|
||||
|
||||
class TextFile:
|
||||
|
||||
def __init__(self, filename):
|
||||
"""
|
||||
init the class and set the filename
|
||||
:param filename:
|
||||
"""
|
||||
assert isinstance(filename, str)
|
||||
self._filename = filename
|
||||
|
||||
def write_text_file(self, file_content):
|
||||
"""
|
||||
write strings to a text file
|
||||
:param file_content: strings which has to been write i a text file
|
||||
:raise ValueError: will be raised, when the content is not a string
|
||||
"""
|
||||
assert isinstance(self._filename, str)
|
||||
with open(self._filename, 'w', newline='\n') as file:
|
||||
file.write(file_content)
|
||||
file.close()
|
||||
|
||||
def check_and_remove_file(self):
|
||||
"""
|
||||
Method remove this file, if the file don't exists the method don't call remove
|
||||
"""
|
||||
if self.exists_file:
|
||||
os.remove(self._filename)
|
||||
|
||||
def read_text_file(self):
|
||||
"""
|
||||
method return and read the content of a text file
|
||||
|
||||
:return: str - file content
|
||||
"""
|
||||
with open(self._filename, newline='\n') as file:
|
||||
return file.read()
|
||||
|
||||
@property
|
||||
def exists_file(self):
|
||||
"""
|
||||
check if this file exists or not
|
||||
:return: true if the file exists, otherwise false
|
||||
"""
|
||||
return os.path.isfile(self._filename)
|
||||
|
||||
@property
|
||||
def get_filename(self):
|
||||
"""
|
||||
get the file Name
|
||||
:return: str
|
||||
"""
|
||||
assert isinstance(self._filename, str)
|
||||
return self._filename
|
||||
Reference in New Issue
Block a user