add read methods to text and json files

This commit is contained in:
stubbfel
2014-05-19 22:03:31 +02:00
parent 4ae29f9304
commit 87192e35dc
4 changed files with 68 additions and 17 deletions

View File

@@ -1,6 +1,6 @@
__author__ = 'stubbfel'
import json
from utilities.textfileutility import TextFile
from utilities.textfile import TextFile
class JsonFile(TextFile):
@@ -12,7 +12,7 @@ class JsonFile(TextFile):
"""
super(JsonFile, self).__init__(filename)
def write_json(self, json_object):
def write_json_file(self, json_object):
"""
write a json object to a text file
:param json_object: json object, which has to be saved. If the object is a string,
@@ -27,4 +27,10 @@ class JsonFile(TextFile):
json_string = json.dumps(json_object, indent=4)
self.write_text_file(json_string)
def read_json_file(self):
"""
method read and convert the content of the file to a json object
:return: dict - json object
"""
return json.loads(self.read_text_file())

View File

@@ -30,6 +30,15 @@ class TextFile:
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):
"""