def combineFilesWithParas(dir_path, filename, targetDir=None, connector="="): """ Combine all files with name filename. Also, parameters will be added as seperated columns in the combined file. The order of parameters of corresponding to the inserted columns is indicated by the file name. """ filenameL = listR.toList(filename) for filename in filenameL: # do the following for each file: # get list of common parameters: allParas = [] for aDir in dirR.listNestedDirContainsFiles(dir_path, filename): allParas.append(listR.readCSESD(path.join(aDir, filename)).keys()) allParas = listR.removeDuplicates(listR.intersect(allParas)) # generate modified file name: if targetDir == None: targetDir=getcwd() new_filename = path.join(targetDir,path.splitext(filename)[0]+"("+",".join(listR.stringizeL(allParas))+")"+path.splitext(filename)[1]) # generate the combined data file: outFile = open(new_filename, "w") for aDir in dirR.listNestedDirContainsFiles(dir_path, filename): to_read = path.join(aDir, filename) # target file: path+name parasD = listR.readCSESD(to_read) # get a dictionary of parameter values para_str = " ".join(listR.stringizeL(listR.getValueListFromDict(allParas, parasD))) # get the associated parameter list in string format inFile = open(to_read, "r") buffer = inFile.readlines() # read target file for aLine in buffer: # concarnate target file with parameter list: outFile.write(para_str+" "+aLine) inFile.close() outFile.close()
def descendDirTreeSharp(baseDir, mustBeDefined, connectionSymbol="=", seperationSymbol=","): """ Return a list of directories and a list of the corresponding var_name:var_value dictionaries under baseDir. The valuse are given using string, like "1" instead of 1. The subdirectaries are so chosen that all variables in mustBeDefined must be defined. Only those directories that barely defined mustBeDefined (sharp) are returned. """ tmp_result = descendDirTree(baseDir, mustBeDefined, connectionSymbol, seperationSymbol) if tmp_result == []: return tmp_result tmp_list = [] for aPath in tmp_result: # for each dir in result while list( listR.biIntersectI( mustBeDefined, readCSED(os.path.basename(aPath), connectionSymbol, seperationSymbol).keys())) == []: # if the lowerest dir definds a variable in the mustBeDefined list aPath = os.path.dirname(aPath) # cut the lowerest dir if aPath == baseDir: break # if it's the shortest dir, return it tmp_list.append(aPath) tmp_list = listR.removeDuplicates(tmp_list) return tmp_list
def descendDirTreeSharp(baseDir, mustBeDefined, connectionSymbol="=", seperationSymbol=","): """ Return a list of directories and a list of the corresponding var_name:var_value dictionaries under baseDir. The valuse are given using string, like "1" instead of 1. The subdirectaries are so chosen that all variables in mustBeDefined must be defined. Only those directories that barely defined mustBeDefined (sharp) are returned. """ tmp_result = descendDirTree(baseDir, mustBeDefined, connectionSymbol, seperationSymbol) if tmp_result==[]: return tmp_result tmp_list = [] for aPath in tmp_result: # for each dir in result while list(listR.biIntersectI(mustBeDefined,readCSED(path.basename(aPath),connectionSymbol,seperationSymbol).keys()))==[]: # if the lowerest dir definds a variable in the mustBeDefined list aPath = path.dirname(aPath) # cut the lowerest dir if aPath == baseDir: break # if it's the shortest dir, return it tmp_list.append(aPath) tmp_list = listR.removeDuplicates(tmp_list) return tmp_list