From 19fe0817ba7c2cd04caea3adfa82d4490288a548 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 14 Dec 2009 07:37:51 -0500 Subject: Transitioned comment to Command format --- libbe/util/encoding.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'libbe/util/encoding.py') diff --git a/libbe/util/encoding.py b/libbe/util/encoding.py index af312c1..434bae7 100644 --- a/libbe/util/encoding.py +++ b/libbe/util/encoding.py @@ -22,6 +22,7 @@ Support input/output/filesystem encodings (e.g. UTF-8). import codecs import locale import sys +import types import libbe if libbe.TESTING == True: @@ -65,5 +66,26 @@ def known_encoding(encoding): except LookupError: return False +def get_file_contents(path, mode='r', encoding=None, decode=False): + if decode == True: + if encoding == None: + encoding = get_filesystem_encoding() + f = codecs.open(path, mode, encoding) + else: + f = open(path, mode) + contents = f.read() + f.close() + return contents + +def set_file_contents(path, contents, mode='w', encoding=None): + if type(value) == types.UnicodeType: + if encoding == None: + encoding = get_filesystem_encoding() + f = codecs.open(path, mode, encoding) + else: + f = open(path, mode) + f.write(contents) + f.close() + if libbe.TESTING == True: suite = doctest.DocTestSuite() -- cgit