fork download
  1. parser = argparse.ArgumentParser()
  2. parser.add_argument("input_file", help='input name of the csv file')
  3. parser.add_argument("output_file", help='name of the output file you want')
  4. parser.add_argument("rows", help='input number of rows you want to divide your file', type=int)
  5. args = parser.parse_args()
  6. location = os.path.join(my_path, args.input_file)
  7. if os.path.exists(location):
  8. with open(location, 'r') as my_inp_file:
  9. my_file_reader = csv.reader(my_inp_file)
  10. data = list(my_file_reader)
  11. row_count = len(data)
  12. if row_count < args.rows:
  13. print("You entered bigger number of rows than file has")
  14. else:
  15. field_names = data[0]
  16. parts = data[1:]
  17. # for x in my_file_reader:
  18. # parts.append(x)
  19. number_of_files = int(math.ceil((row_count-1)/args.rows))
  20. for a in range(number_of_files):
  21. out = open((args.output_file + str(a) + ".csv"), "w")
  22. spamwriter = csv.writer(out)
  23. spamwriter.writerow(field_names)
  24. print("====================")
  25. for q in range(args.rows):
  26. try:
  27. spamwriter.writerow(parts[q])
  28. print(parts[q])
  29. del parts[q]
  30. except IndexError:
  31. continue
  32.  
  33. out.close()
  34.  
  35. else:
  36. print("There is no such file...")
Runtime error #stdin #stdout #stderr 0.01s 28384KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
NameError: name 'argparse' is not defined