def email_domain_extract(): if not os.path.exists('analytics'): os.mkdir('analytics') if os.path.exists('analytics/email'): shutil.rmtree('analytics/email') with open('studentinfo_cs384.csv', newline='') as csvfile: reader = csv.DictReader(csvfile) if not os.path.exists('analytics/email'): os.mkdir('analytics/email') for row in reader: l = list(row.values()) head = list(row.keys()) if '@' in row['email'] and '.' in row['email']: domain = row['email'].split('@')[1].split('.')[0] with open('analytics/email/'+domain+ '.csv', mode = 'a') as f: f_write = csv.writer(f, delimiter=',',lineterminator='\r') if os.path.getsize('analytics/email/'+ domain + '.csv')==0: f_write.writerow(head) f_write.writerow(l) f.close() else: with open('analytics/email/'+'misc'+ '.csv', mode = 'a') as f: f_write = csv.writer(f, delimiter=',',lineterminator='\r') if os.path.getsize('analytics/email/'+ domain + '.csv')==0: f_write.writerow(head) f_write.writerow(l) f.close() csvfile.close()