class File: """ A binary r/w file container that transparently abstracts file descriptors away. You just read and write data. Nonexistent files will be created, including any directories if necessary. """ # def __init__(self, path, str encoding="utf-8"): def __init__(self, path, encoding="utf-8"): self.encoding = encoding self._print = Output("over.File", timestamp=True) if path[0] == "~": self.path = os.path.join(os.getenv("HOME"), path[2:]) else: self.path = path if not os.path.isfile(self.path): if os.path.exists(self.path): self._print("path §r%s§/ exists but is not a file" %(self.path), prefix.fail, exc=GeneralError) else: dirname = os.path.dirname(self.path) if dirname and not os.path.isdir(dirname): self._print("creating directory §B%s§/" %(dirname), prefix.start) os.makedirs(dirname) # create the file touch(self.path) @property def data(self): """ Reads the file and returns the contents. """ fd = open(self.path, encoding=self.encoding) data = fd.read() fd.close() return data @data.setter def data(self, data): """ Writes data into the file. """ fd = open(self.path, "w", encoding=self.encoding) fd.write(data) fd.close()