def text_opt(self, src_file_text, des_file_text, key_word_list): des_text = des_file_text if self.opt == u'增加': for key_word in key_word_list: print u'当前处理:%s' % key_word key_text, tag_name = self.get_key_text(src_file_text, key_word) try: self.get_key_text(des_file_text, key_word) except: pass else: print u'目标文件中已存在"%s"' % key_word exception.file_fallback(self.des_path) exception.fallback(conf.reply) sys.exit(1) des_text = self.add_to_file(des_text, key_text, tag_name) print u'增加%s成功' % key_word #替换计数行 #des_text = self.chg_rec_num(src_file_text, des_text, self.format_str_list[1]) elif self.opt == u'删除': for key_word in key_word_list: print u'当前处理:%s' % key_word key_text, tag_name = self.get_key_text(des_file_text, key_word) des_text = self.del_from_file(des_text, key_text, tag_name) #替换计数行 #des_text = chg_rec_num(src_file_text, des_text, self.format_str_list[1]) elif self.opt == u'修改': for key_word in key_word_list: print u'当前处理:%s' % key_word src_key_text = self.get_key_text(src_file_text, key_word)[0] des_key_text = self.get_key_text(des_file_text, key_word)[0] des_text = self.rep_file(des_text, des_key_text, src_key_text) else: print u'选项设置错误,请检查' exit(1) return des_text
def file_chg(self): des_file_text = get_file_text(self.des_path) src_file_text = get_file_text(self.src_path) #key_text_list = self.get_key_text_list(self.format_str_list) try: new_text = self.text_opt(src_file_text, des_file_text, self.key_word_list) except RecNumError: print u'计数行有多个相同项无法处理' exception.file_fallback(self.des_path) exception.fallback(conf.reply) #exception.dat_fallback() #exception.unlock_dat_file() exit(1) except TextError: print u'有多个相同标签,无法处理' exception.file_fallback(self.des_path) exception.fallback(conf.reply) #exception.dat_fallback() #exception.unlock_dat_file() exit(1) except: print self.des_path + u' 操作失败,请检查确认操作内容是否填写错误' exception.file_fallback(self.des_path) exception.fallback(conf.reply) #exception.dat_fallback() #exception.unlock_dat_file() exit(1) else: #写入新文件 new_des_file_obj = open(self.des_path, 'w+b') try: new_des_file_obj.write(new_text) except: print u'写入新文件\n' + self.des_path + u'失败' exception.file_fallback(self.des_path) exception.fallback(conf.reply) #exception.dat_fallback() #exception.unlock_dat_file() exit(1) finally: new_des_file_obj.close()