import os import glob import shutil import re # Python 2.7.11 written by Ali Khalighifar on July 14, 2017 # This script seperates each replicate from the outputs of EstimateS and creates a new table (text file) of them. baseDir = 'Working Directory' finalDir = baseDir + '\Dest_result' os.chdir(baseDir) if (not(os.path.isdir(finalDir))): os.mkdir(finalDir) # Splitting each replicate of EstimateS outputs by finding the beginning of the replicate start_split = 'EstimateS (Version 9.1.0), Copyright R. K. Colwell: http://purl.oclc.org/estimates\nDiversity Output from Input File: ' txtFiles = [txt_file for txt_file in glob.iglob ('*.txt')] a = 1 for output in txtFiles: result = open (output, 'U') lines = result.read() split_up = lines.split(start_split) del split_up[0] print len(split_up) first = str.split(output, '.txt') filenames = first[0] filenames = re.findall(r'\d{1,4}_days', filenames) filenames = filenames[0] filenames = filenames.replace('_', '') print filenames for i,name in enumerate(split_up): # "nine" is the code for the error abundance and "High" is the code for the mean abundance of real Species f = open('nine'+'High'+filenames+'Rep'+str(i+a)+'.txt','w') f.write(name+'\n') f.close() source = baseDir+'\\'+'nine'+'High'+filenames+'Rep'+str(i+a)+'.txt' dest = finalDir + '\\' +'nine'+'High'+filenames if (not(os.path.isdir(dest))): os.mkdir(dest) shutil.move(source, dest)