__author__ = 'dmy' ## store iPso file data import os import sys import distutils.dir_util import shutil class iPsoData: def __init__(self, filePath): self.__basicInfo = [] self.__balanceInfo = [] self.__line = [] self.__trans = [] self.__nodePwer = [] self.__gen = [] self.__reGen = [] self.__voltLimit = [] self.__ground = [] self.__isAvailable = False self.__read__(filePath) pass def __read__(self, filePath): if not os.path.exists(filePath): self.__isAvailable = True cur = 0 with open(filePath) as f: # 把iPso文件的数据读到内存中 line = f.readline() while line: formatedLine = line.strip() if line == '': line = f.readline() continue sep = formatedLine.split(' ') if len(sep) < 1: line = f.readline() continue if sep[0] == '0': cur += 1 line = f.readline() continue try: floatSep = [float(s) for s in sep] except: print(sep) if cur == 0: self.__basicInfo.append(floatSep) if cur == 1: self.__balanceInfo.append(floatSep) if cur == 2: self.__line.append(floatSep) if cur == 3: self.__trans.append(floatSep) if cur == 5: self.__ground.append(floatSep) if cur == 8: self.__nodePwer.append(floatSep) if cur == 9: self.__gen.append(floatSep) if cur == 11: self.__reGen.append(floatSep) if cur == 13: self.__voltLimit.append(floatSep) line = f.readline() def GetBasicInfo(self): return self.__basicInfo def GetBalance(self): return self.__balanceInfo def GetLine(self): return self.__line def GetTrans(self): return self.__trans def GetNodePower(self): return self.__nodePwer def GetGen(self): return self.__gen def GetReactiveGen(self): return self.__reGen def GetVoltLimit(self): return self.__voltLimit def GetGround(self): return self.__ground class MergeReconfigFile: # 合并重构用文件 def __init__(self, fileList): self.__basicInfo = [[0, 0]] #节点数 支路数 self.__line = [] self.__trans = [] self.__node = [] self.__reverseDic = {} self.__mergedIpsoBalance = [] self.__mergedIpsoLine = [] self.__mergedIpsoTrans = [] self.__mergedIpsoLoad = [] self.__mergedIpsoGround = [] self.__mergedNodeNameDic = {} # self.__addNumDic = {} #计算每个文件在新文件中的偏移量 self.__balanceNodeOffset=[]#记录每条线路头节点在整个合并文件中的位置 self.__merge(fileList) def __merge(self, fileList): indLine = 1 #序号 indTrans=1 indNode = 1 indGround = 1 addNum = 1 # offsetNodeNum = {} #记录每个一个文件在新文件里节点的偏移量 for f in fileList: ipsodata = iPsoData(f) #节点数 可重构支路数 self.__basicInfo[0][0] += len(ipsodata.GetNodePower()) self.__basicInfo[0][1] += len(ipsodata.GetLine()) self.__basicInfo[0][1] += len(ipsodata.GetTrans()) #序号 变电站节点编号 for s in ipsodata.GetBalance(): self.__balanceNodeOffset.append(s[1]+addNum) #序号 支路节点I 支路节点J for l in ipsodata.GetLine(): self.__mergedIpsoLine.append([indLine, l[1] + addNum, l[2] + addNum, l[3], l[4], l[5], l[6]]) self.__reverseDic[l[1] + addNum] = l[1]#反过来查是哪个节点 self.__reverseDic[l[2] + addNum] = l[2] indLine += 1 for t in ipsodata.GetTrans(): self.__mergedIpsoTrans.append([indTrans,1, t[2] + addNum, t[3] + addNum, t[4], t[5], 1, 1, 1, 1]) self.__reverseDic[t[2] + addNum] = t[2] self.__reverseDic[t[3] + addNum] = t[3] indTrans += 1 #接地支路 for g in ipsodata.GetGround(): self.__mergedIpsoGround.append([indGround, g[1] + addNum, g[2], g[3]]) #先给PG和QG赋值 node = ipsodata.GetNodePower() #序号 节点编号 节点有功 节点无功 负荷有功 负荷无功 for n in ipsodata.GetNodePower(): self.__mergedIpsoLoad.append([indNode, n[1], n[2] + addNum, n[3], n[4], n[5], n[6], n[7], 0, 0]) indNode += 1 # self.__addNumDic[lineName] = len(ipsodata.GetNodePower()) #每条线路的节点数 # offsetNodeNum[lineName] = addNum #记录的是与之前线路的偏移量 addNum += len(ipsodata.GetNodePower()) #用一个总的头节点把之前所有线路的头节点连接起来 for b in self.__balanceNodeOffset: self.__mergedIpsoLine.append ([indLine, 1 , 1 + b, 0.01, 0.01, 0, 0]) indLine+=1 def __GetLineName(self, filePath): baseName = os.path.basename(filePath) fileName = os.path.splitext(baseName)[0] lineName = fileName.split('_')[1] return lineName def __Swap(self, a, b): c = b b = a a = c return (a, b) def Output(self, fileOutDir): #输出到文件 #输出节点映射 self.__outputNodeName(fileOutDir) with open(fileOutDir + '_Merged.txt', 'w') as f: for b in self.__basicInfo: f.write('%d %d 1 1e-5 1\n' % (b[0], b[1])) f.write('0\n') f.write('1 1 1.5\n') f.write('0\n') for b in self.__mergedIpsoLine: f.write('%d %d %d %f %f %f %f\n' % (b[0], b[1], b[2],b[3],b[4],b[5],b[6])) f.write('0\n') for b in self.__mergedIpsoTrans: f.write('%d %d %d %d %f %f %f %f %f %f\n' % (b[0], b[1], b[2],b[3],b[4],b[5],b[6],b[7],b[8],b[9])) f.write('0\n') f.write('0\n') for g in self.__mergedIpsoGround: f.write('%d %d %f %f\n' % (g[0], g[1], b[2],b[3])) f.write('0\n') f.write('0\n') f.write('0\n') for n in self.__node: f.write('%d %d %d %d %.10f %.10f %.10f %.10f %.10f %.10f\n' % (n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], n[8], n[9])) #负荷单位是kW f.write('0\n') f.write('1 1 1 1 -100 100 0 0 0 0\n') f.write('0\n') f.write('0\n') f.write('1 1 1 1 1.05 -100 100 0 0 0 0\n') f.write('0\n') f.write('0\n') f.write('1 1 0.999 1.001\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') with open(fileOutDir, 'w') as f: for b in self.__basicInfo: f.write('%d,%d' % (b[0], b[1])) f.write(',1,1,1\n') f.write('0\n') for s in self.__mergedIpsoBalance: f.write('%d,%d,%.10f\n' % (s[0], s[1], s[2])) f.write('0\n') for b in self.__mergedIpsoLine: f.write('%d,%d,%d,%.10f,%.10f,%.10f,%.10f\n' % (b[0], b[1], b[2], b[3], b[4], b[5], b[6])) f.write('0\n') f.write('0\n') f.write('0\n') for g in self.__mergedIpsoGround: f.write('%d,%d,%.10f,%.10f\n' % (g[0], g[1], g[2], g[3])) f.write('0\n') f.write('0\n') f.write('0\n') for b in self.__mergedIpsoLoad: f.write('%d,%d,%d,%d,%.10f,%.10f,%.10f,%.10f,%.10f,%.10f\n' % ( b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9])) f.write('0\n') f.write('0\n') f.write('0\n') for b in self.__mergedIpsoBalance: f.write('%d,%d,%d,%d,%.10f,%.10f,%.10f\n' % (b[0], 1, b[1], 1, 1, -100, 100)) f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') def GetReverseMap(self): #通过新的节点号返回原来的节点号 return self.__reverseDic; def GetCanReconfig(self): return self.__canReconfig class AddLoadToReconfigFile: # 把负荷给加上 def __init__(self, ReconfigFilepath): dirPath = os.path.dirname(ReconfigFilepath) reconfigData = ReconfigFileData(ReconfigFilepath) node = reconfigData.GetNodePowerInfo() PDFilePath = dirPath + '/PD.txt' with open(PDFilePath) as f: for i, line in enumerate(f): newLine = line.split() if newLine == '': continue node[i][4] = float(newLine[-1]) QDFilePath = dirPath + '/QD.txt' with open(QDFilePath) as f: for i, line in enumerate(f): newLine = line.split() if newLine == '': continue node[i][5] = float(newLine[-1]) dirName = os.path.basename(dirPath) newFilePath = dirPath + '/' + dirName + '_Reconfigure_newFile.txt' with open(newFilePath, 'w') as f: for b in reconfigData.GetBasicInfo(): f.write('%d %d\n' % (b[0], b[1])) f.write('0\n') for b in reconfigData.GetSubstation(): f.write('%d %d\n' % (b[0], b[1])) f.write('0\n') for b in reconfigData.GetBranchInfo(): f.write('%d %d %d\n' % (b[0], b[1], b[2])) f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') #在发电机处加负荷 for i in reconfigData.GetSubstation(): reconfigData.GetNodePowerInfo()[i[1] - 1][2] = 100 reconfigData.GetNodePowerInfo()[i[1] - 1][3] = 100 for b in reconfigData.GetNodePowerInfo(): f.write( '%d %d %.10f %.10f %.10f %.10f\n' % (b[0], b[1], b[2] * 1e4, b[3] * 1e4, b[4] * 1e4, b[5] * 1e4)) f.write('0\n') f.write('0\n') for b in reconfigData.GetInterLineInfo(): f.write('%d %d %d %d\n' % (b[0], b[1], b[2], b[3])) f.write('0\n') f.write('0\n') class AddLoadToIpsoFile: # 把负荷给加上 def __init__(self, ipsoFilepath): dirPath = os.path.dirname(ipsoFilepath) ipsoData = iPsoData(ipsoFilepath) node = ipsoData.GetNodePower() PDFilePath = dirPath + '/PD.txt' with open(PDFilePath) as f: for i, line in enumerate(f): newLine = line.split() if newLine == '': continue node[i][6] = float(newLine[-1]) QDFilePath = dirPath + '/QD.txt' with open(QDFilePath) as f: for i, line in enumerate(f): newLine = line.split() if newLine == '': continue node[i][7] = float(newLine[-1]) dirName = os.path.basename(dirPath) newFilePath = dirPath + '/' + dirName + '_iPso_newFile.txt' with open(newFilePath, 'w') as f: for b in ipsoData.GetBasicInfo(): f.write('%d\t%d\t%d\t%.10f\t%.10f\n' % (b[0], b[1], 1, b[3], b[4])) f.write('0\n') for b in ipsoData.GetBalance(): f.write('%d\t%d\t%.10f\n' % (b[0], b[1], b[2])) f.write('0\n') for b in ipsoData.GetLine(): f.write('%d\t%d\t%d\t%.10f\t%.10f\t%.10f\t%.10f\n' % (b[0], b[1], b[2], b[3], b[4], b[5], b[6])) f.write('0\n') for b in ipsoData.GetTrans(): f.write('%d\t%d\t%d\t%d\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n' % ( b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9])) f.write('0\n') f.write('0\n') for b in ipsoData.GetGround(): f.write('%d\t%d\t%.10f\t%.10f\n' % (b[0], b[1], b[2], b[3])) f.write('0\n') f.write('0\n') f.write('0\n') for b in ipsoData.GetNodePower(): f.write('%d\t%d\t%d\t%d\t%.10f\t%.10f\t%.10f\t%.10f\t%f\t%.10f\n' % ( b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9])) f.write('0\n') for b in ipsoData.GetGen(): f.write('%d\t%d\t%d\t%d\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n' % ( b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9])) f.write('0\n') f.write('0\n') for b in ipsoData.GetReactiveGen(): f.write('%d\t%d\t%d\t%d\t%.10f\t%.10f\t%.10f\n' % (b[0], b[1], b[2], b[3], b[4], b[5], b[6])) f.write('0\n') f.write('0\n') for b in ipsoData.GetVoltLimit(): f.write('%d\t%d\t%.10f\t%.10f\n' % (b[0], b[1], b[2], b[3])) f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') class NodeInfo: def __init__(self, lineInfoPath, transInfoPath): self.__infoDic = {} with open(lineInfoPath) as f: for line in f: newLine = line.strip() if newLine == '': continue sep = newLine.split(',') if len(sep) < 4: continue nodeI = int(sep[1]) nodeJ = int(sep[2]) info = sep[3].split('-') if len(info) < 2: continue nodeIinfo = info[0].strip() nodeJinfo = info[1].strip() if not nodeI in self.__infoDic.keys(): self.__infoDic[nodeI] = nodeIinfo if not nodeJ in self.__infoDic.keys(): self.__infoDic[nodeJ] = nodeJinfo with open(transInfoPath) as f: for line in f: newLine = line.strip() if newLine == '': continue sep = newLine.split(',') if len(sep) < 4: continue nodeI = int(sep[1]) info = sep[4].strip() if not nodeI in self.__infoDic.keys(): self.__infoDic[nodeI] = info nodeJ = int(sep[2]) if not nodeJ in self.__infoDic.keys(): self.__infoDic[nodeJ] = info def GetNodeName(self, nodeI): if nodeI in self.__infoDic.keys(): return self.__infoDic[nodeI] else: return "" class AddLoadtoFileBatch: def __init__(self, baseDir, AddLoadInterface=AddLoadToIpsoFile): dirList = os.listdir(baseDir) for d in dirList: absDir = baseDir + '/' + d if not os.path.isdir(absDir): continue # 看有没有PD.txt 和 QD.txt PDFilePath = absDir + '/PD.txt' QDFilePath = absDir + '/QD.txt' if not os.path.exists(PDFilePath) or not os.path.exists(QDFilePath): continue if AddLoadInterface is AddLoadToReconfigFile: absDatFile = absDir + '/iPso_' + d + '_DisnetReconfig.txt' if AddLoadInterface is AddLoadToIpsoFile: absDatFile = absDir + '/iPso_' + d + '.txt' if not os.path.exists(absDatFile): continue AddLoadInterface(absDatFile) print('完成 %s' % (absDatFile)) class LoadMapSingleton: PloadDic = {} QloadDic = {} class NodeLoadMap: # 依据变压器串号对负荷进行映射 def __init__(self, manyFileDir, singleFileDir, AddLoadInterface=AddLoadToIpsoFile): #manyFileDir是分开的计算文件目录的上级目录 singleFileDir是合并后并形成重构的文件的目录 dirList = os.listdir(manyFileDir) PloadDic = {} QloadDic = {} if len(LoadMapSingleton.PloadDic.keys()) == 0 or len(LoadMapSingleton.QloadDic.keys()) == 0: for d in dirList: absDir = manyFileDir + '/' + d if not os.path.exists(absDir): continue #看看有没有负荷文件 PDFilePath = absDir + '/PD.txt' QDFilePath = absDir + '/QD.txt' if not os.path.exists(PDFilePath) or not os.path.exists(QDFilePath): continue print('利用了%s中的负荷' % absDir) #把负荷信息读出来 PD = [] with open(PDFilePath) as f: for line in f: newLine = line.strip() if len(newLine) == 0: continue PD.append(float(newLine)) QD = [] with open(QDFilePath) as f: for line in f: newLine = line.strip() if len(newLine) == 0: continue #if(float(newLine)<0): # print(newLine) QD.append(float(newLine)) #找到变压器信息文件 manyTransFilePath = absDir + '/iPso_' + d + '_TransformerInfor.txt' if not os.path.exists(manyTransFilePath): continue with open(manyTransFilePath) as f: for line in f: newLine = line.strip() if len(newLine) == 0: continue sep = newLine.split(',') if len(sep) >= 6: ID = sep[5].strip() loadSideNum = int(sep[2]) PloadDic[ID] = PD[loadSideNum - 1] QloadDic[ID] = QD[loadSideNum - 1] LoadMapSingleton.PloadDic = PloadDic LoadMapSingleton.QloadDic = QloadDic else: PloadDic = LoadMapSingleton.PloadDic QloadDic = LoadMapSingleton.QloadDic #开始处理合并红的文件。方法是在合并的文件夹下面生成PD.txt和QD.txt,然后利用AddLoadToIpso类来添加。 singleTransFile = singleFileDir + '/iPso_' + os.path.basename(singleFileDir) + '_TransformerInfor.txt' if not os.path.exists(singleTransFile): print('合并后的变压器信息文件不存在') return if AddLoadInterface is AddLoadToIpsoFile: absDatFile = singleFileDir + '/iPso_' + os.path.basename(singleFileDir) + '.txt' data = iPsoData(absDatFile) basicInfo = data.GetBasicInfo() totalNum = int(basicInfo[0][0]) if AddLoadInterface is AddLoadToReconfigFile: absDatFile = singleFileDir + '/iPso_' + os.path.basename(singleFileDir) + '_DisnetReconfig.txt' data = ReconfigFileData(absDatFile) basicInfo = data.GetBasicInfo() totalNum = int(basicInfo[0][0]) singlePD = [0 for i in range(totalNum)] singleQD = [0 for i in range(totalNum)] totalPD = 0 with open(singleTransFile) as f: for line in f: newLine = line.strip() if len(newLine) == 0: continue sep = newLine.split(',') if len(sep) >= 6: ID = sep[5].strip() loadSideNum = int(sep[2]) if ID in PloadDic.keys(): singlePD[loadSideNum - 1] = PloadDic[ID] singleQD[loadSideNum - 1] = QloadDic[ID] else: singlePD[loadSideNum - 1] = 1e-8 singleQD[loadSideNum - 1] = 1e-8 totalPD += singlePD[loadSideNum - 1] singlePDFilePath = singleFileDir + '/PD.txt' singleQDFilePath = singleFileDir + '/QD.txt' PDf = open(singlePDFilePath, 'w') QDf = open(singleQDFilePath, 'w') if not PDf or not QDf: print('无法创建文件') return for i in range(len(singlePD)): PDf.write('%.10f\n' % singlePD[i]) QDf.write('%.10f\n' % singleQD[i]) if PDf: PDf.close() if QDf: QDf.close() AddLoadInterface(absDatFile) print('完成%s中负荷的添加' % (absDatFile)) print('总的有功负荷为%f' % (totalPD)) class ReconfigFileData: # 读入祝靖用的重构文件格式 def __init__(self, filePath): currBlock = 1 self.__basicInfo = [] self.__subStationInfo = [] self.__branchInfo = [] self.__nodePowerInfo = [] self.__interLineInfo = [] with open(filePath) as f: for line in f: newLine = line.strip() sep = line.split(' ') if len(sep) > 0: if int(sep[0]) == 0: currBlock += 1 continue if currBlock == 1: self.__basicInfo.append([int(sep[0]), int(sep[1])]) continue if currBlock == 2: self.__subStationInfo.append([int(s) for s in sep]) continue if currBlock == 3: self.__branchInfo.append([int(s) for s in sep]) continue if currBlock == 7: self.__nodePowerInfo.append( [int(sep[0]), int(sep[1]), float(sep[2]), float(sep[3]), float(sep[4]), float(sep[5])]) continue if currBlock == 9: t = [int(s) for s in sep[:-1]] t.append(float(sep[-1])) self.__interLineInfo.append(t) def GetBasicInfo(self): return self.__basicInfo def GetSubstation(self): return self.__subStationInfo def GetBranchInfo(self): return self.__branchInfo def GetNodePowerInfo(self): return self.__nodePowerInfo def GetInterLineInfo(self): return self.__interLineInfo def SaveTo(self, filePath): with open(filePath, 'w') as f: for i in self.__basicInfo: f.write('%d %d\n' % (i[0], i[1])) f.write('0\n') for i in self.__subStationInfo: f.write('%d %d\n' % (i[0], i[1])) f.write('0\n') for i in self.__branchInfo: f.write('%d %d %d\n' % (i[0], i[1], i[2])) f.write('0\n') f.write('0\n') f.write('0\n') f.write('0\n') for i in self.__nodePowerInfo: f.write('%d %d %f %f %f %f\n' % (i[0], i[1], i[2], i[3], i[4], i[5])) f.write('0\n') f.write('0\n') for i in self.__interLineInfo: f.write('%d %d %d %d\n' % (i[0], i[1], i[2], i[3])) f.write('0\n') class StdiPsoToZJ: # 把iPso的文件转换为祝靖要的格式 def __init__(self, iPsoDatFile): iPsoDat = iPsoData(iPsoDatFile) self.__madeStructure = [] madeStructure = self.__madeStructure basicInfo = iPsoDat.GetBasicInfo() madeStructure.append([basicInfo[0][0], basicInfo[0][1]]) madeStructure.append([0]) balanceInfo = iPsoDat.GetBalance() for i in balanceInfo: madeStructure.append([i[0], i[1]]) madeStructure.append([0]) lineInfo = iPsoDat.GetLine() for i in lineInfo: madeStructure.append([i[0], i[1], i[2]]) transInfo = iPsoDat.GetTrans() for i in transInfo: madeStructure.append([i[0], i[2], i[3]]) madeStructure.append([0]) madeStructure.append([0]) madeStructure.append([0]) madeStructure.append([0]) loadInfo = iPsoDat.GetNodePower() loadInfo[int(basicInfo[0][1])][4] = 100 loadInfo[int(basicInfo[0][1])][5] = 100 for i in loadInfo: madeStructure.append([i[0], i[2], i[4], i[5], i[6], i[7]]) madeStructure.append([0]) madeStructure.append([0]) #可重构支路 for i in lineInfo: madeStructure.append([i[0], i[1], i[2], 1]) madeStructure.append([0]) def Save(self, saveTo): with open(saveTo, 'w') as f: seperator = '\t' madeStructure = self.__madeStructure for i in madeStructure: strI = [str(j) for j in i] newLine = seperator.join(strI) f.write(newLine) f.write('\n') class NodeLoadMapBatch: def __init__(self, mappingFileDir, mappedFileDir, AddLoadInterface=AddLoadToIpsoFile): # mappingFileDir是有负荷的文件夹的上级目录,mappedFileDir是被填充的文件夹的上级目录 dirList = os.listdir(mappedFileDir) for d in dirList: absDir = mappedFileDir + '/' + d if os.path.isdir(absDir): NodeLoadMap(mappingFileDir, absDir, AddLoadInterface) class ConnectionSwitch: def __init__(self, switchFile): if not os.path.exists(switchFile): raise Exception('swtich file not exists!') self.__switch = [] with open(switchFile) as f: for line in f: newLine = line.strip() sep = newLine.split(',') if len(sep) == 0: continue if len(sep) == 4: self.__switch.append(sep) def GetSwtich(self): return self.__switch class BuildReconfigurationArea: # 依据重构开关吧,把需要重构的文件准备好,拷贝到一个目录下 def __init__(self, baseDir, switchDir, distFilePoolDir): #baseDir 是拷贝到的文件夹,switchDir是重构开关的文件夹,都是txt文件 distFilePoolDir是所有配网文件的目录 self.__getDistFiles(distFilePoolDir) allSwitchFile = self.__GetAllSwtichFile(switchDir) for a in allSwitchFile: print('正在利用%s' % a) absDir = baseDir + '/' + os.path.splitext(os.path.basename(a))[0] #先删除原来就有的目录 #if os.path.exists(absDir): # pass #shutil.rmtree(absDir) #建立一个新文件夹 if not os.path.exists(absDir): distutils.dir_util.mkpath(absDir) switch = ConnectionSwitch(a) dupLineNam = [] #有重复的线路文件名称 for s in switch.GetSwtich(): dupLineNam.append(s[0]) dupLineNam.append(s[2]) uniqueLineName = set(dupLineNam) for u in uniqueLineName: try: copyTargetDir = absDir + '/' + os.path.basename(self.__distFileDic[u]) if os.path.exists(copyTargetDir): shutil.rmtree(copyTargetDir) except Exception as e: print(a) print(e) srcDirPath = self.__distFileDic[u] shutil.copytree(srcDirPath, copyTargetDir, True) #然后再考新文件 #拷贝联络开关文件 shutil.copy(a, absDir + '/breaker.txt') print('Build ReconfigurationArea Finished!') def __getDistFiles(self, distFilePoolDir): dirList = os.listdir(distFilePoolDir) self.__distFileDic = {} for d in dirList: absDir = distFilePoolDir + '/' + d if not os.path.isdir(absDir): continue lineName = self.__getLineName(absDir) self.__distFileDic[lineName] = absDir def __getLineName(self, linePath): baseName = os.path.basename(linePath) fileName = os.path.splitext(baseName)[0] lineName = fileName.split('_')[0] return lineName def __GetAllSwtichFile(self, switchDir): dirList = os.listdir(switchDir) for d in dirList: absFilePath = switchDir + '/' + d if os.path.isfile(absFilePath): yield absFilePath class TargetDir: def __init__(self, dir): dirList = os.listdir(dir) opFileList = [] for d in dirList: opDirPath = dir + '/' + d # 操作文件夹 if os.path.isdir(opDirPath): opFile = opDirPath + '/iPso_' + d + '.txt' if not os.path.exists(opFile): print('文件 %s 不存在' % (opFile)) opFileList.append(opFile) self.__opFileList = opFileList def GetOpFileList(self): print('文件顺序为:') for o in self.__opFileList: print(o) return self.__opFileList class MergeReconfigBatch: def __init__(self, baseDir): listDir = os.listdir(baseDir) for l in listDir: absDir = baseDir + '/' + l if not os.path.isdir(absDir): continue # 清除旧文件 self.__delOldFiles(absDir) opDirList = TargetDir(absDir) a = MergeReconfigFile(opDirList.GetOpFileList()) if a.GetCanReconfig(): a.Output(absDir + '/合并后iPso文件.txt') else: print('%s不满足重构条件' % absDir) def __delOldFiles(self, dir): files = os.listdir(dir) for f in files: absFile = dir + '/' + f if not os.path.isfile(absFile): continue if f == 'breaker.txt': continue if f == 'output.txt': continue os.remove(absFile) # opDirList=TargetDir(r'C:\lz/test3') #a=MergeReconfigFile(opDirList.GetOpFileList()) #a.Output('c:/lz/test3/合并后iPso文件.txt') class IslandedReconfig: # 有孤岛的重构文件 def __init__(self, origFile, removedLine): reconfigFile = ReconfigFileData(origFile) removedLineDic = {} with open(removedLine) as f: for line in f: newLine = line.strip() sep = newLine.split(' ') if len(sep) == 0: continue if len(sep) >= 3: key = sep[0] + '_' + sep[1] removedLineDic[key] = 1 branch = reconfigFile.GetBranchInfo() for i in branch[:]: key = str(i[1]) + '_' + str(i[2]) if key in removedLineDic.keys(): branch.remove(i) dirName = os.path.dirname(origFile) reconfigFile.SaveTo(dirName + '/断线后Reconfig.txt')