input_folder = 'C:\\Users\\ermwebb\\Dropbox\\IU\\Complex Systems Project- Filippo Radicchi\\Yearly MLB Stats\\' output_folder = 'C:\\Users\\ermwebb\\Dropbox\\IU\\Complex Systems Project- Filippo Radicchi\\Yearly Usernames\\' batter_ending = 'usernames_nonpitchers.txt' pitcher_ending ='usernames_pitchers.txt' import os for input_file in os.listdir(input_folder): print input_file start = False end = False batter_names = [] pitcher_names=[] c=3 reader = open(input_folder+input_file,'r') year = input_file[0:4] print year name=[] last_line=[] while not start: line = reader.readline() if 'Player Standard Batting' in line: start = True while not end: line = reader.readline() # print line if 'MLB ') name = line[start_name_index:end_name_index] last_line = line reader.close() writer = open(output_folder+year+batter_ending,'w') writer.writelines(batter_names) writer.close() writer = open(output_folder+year+pitcher_ending,'w') writer.writelines(pitcher_names) writer.close()