def writeFile(file_path,content,encoding="utf-8",compress=None): #try: if True==compress: f = open(file=file_path, mode="wb"); bytes_content = bytes(content.encode(encoding)); bytes_content = zlib.compress(bytes_content) f.write(bytes_content); else: try: f = open(file=file_path, mode="w", encoding=encoding); except: f = open(file=file_path, mode="x", encoding=encoding); content = bytes(content.encode(encoding)).decode(encoding) #tr = vr.decode(encoding='utf-8')##utf-8 string to unicode f.write(content); f.close(); #except: print("");
def hash_object(self, data, obj_type, write=True): """ Computes hash of object data of a given type. If 'write' is True writes to object store. Returns ------- SHA-1 object as a hash string """ from distutils.file_util import write_file header = '{} {}'.format(obj_type, len(data)).encode() full_data = header + b'x\00' + data sha1 = hashlib.sha1(full_data).hexdigest() if write: path = os.path.join('.git', 'objects', sha1[:2], sha1[2:]) if not os.path.exists(path): os.makedirs(os.path.dirname(path), exist_ok=True) write_file(path, zlib.compress(full_data)) return sha1
def writeFile(fileName, filePath, content,encoding="utf-8",compress=None): if None==filePath: full_path = fileName; else: full_path = os.path.join(filePath, fileName) if False==os.path.exists(full_path): os.makedirs(filePath); try: if True==compress: f = os.open(file=full_path, mode="wb+"); bytes_content = bytes(content.encode(encoding)); bytes_content = zlib.compress(bytes_content) f.write(bytes_content); else: f = open(file=full_path, mode="w+", encoding=encoding); content = bytes(content.encode(encoding)).decode(encoding) f.write(content); f.close(); except: raise IOError;