文件操作
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def load_file(self, filename): #读文件 data = {} if os.path.exists(filename) and os.path.getsize(filename) > 0: try: with open(filename, "rb") as r: tmp = r.read() if tmp: data = json.loads(tmp) except Exception, ex: self.logger.error(ex) return data # save init to file def save_file(self, filename, data): # 写文件 try: with open(filename, "wb") as f: json.dump(data, f, indent=4, sort_keys=True, default=str) #f.write(json.dumps(data, indent=4, sort_keys=True, default=str)) return True except Exception, ex: self.logger.error(ex) return False