tutorial_8.py 940 B

12345678910111213141516171819202122
  1. def blood_group():
  2. if not os.path.exists('analytics'):
  3. os.mkdir('analytics')
  4. if os.path.exists('analytics/blood_group'):
  5. shutil.rmtree('analytics/blood_group')
  6. with open('studentinfo_cs384.csv', newline='') as csvfile:
  7. reader = csv.DictReader(csvfile)
  8. if not os.path.exists('analytics/blood_group'):
  9. os.mkdir('analytics/blood_group')
  10. for row in reader:
  11. l = list(row.values())
  12. head = list(row.keys())
  13. with open('analytics/blood_group/'+row['blood_group']+ '.csv', mode = 'a') as f:
  14. f_write = csv.writer(f, delimiter=',',lineterminator='\r')
  15. if os.path.getsize('analytics/blood_group/'+row['blood_group'] + '.csv')==0:
  16. f_write.writerow(head)
  17. f_write.writerow(l)
  18. f.close()
  19. csvfile.close()
  20. # Create the new file here and also sort it in this function only.