- Don't use string literals as comments. PEP 8 explains how to use comments.
- Docstrings should use
""" rather than ''', as described in PEP 257. Also your docstring doesn't need the "-", and should probably be rephrased slightly to fit on one line.
- Close files,
#fb.close() shows you went out of your way to make bad code. Without fb.close or wrapping open in a with, the file is not guaranteed to be closed. I personally prefer with to fb.close, as described here.
- Personally, rather than over-riding your files \$n\$ times, I'd use
collections.defaultdict, to group all your files into their rows.
- You may want to change
get_file_path, to be based of __file__. Or leave your path to be relative, as it'll default to that behavior.
import os
import csv
from collections import defaultdict
FILE_DIR = os.path.dirname(os.path.abspath(__file__))
def get_file_path(filename):
return os.path.join(FILE_DIR, filename)
file_path = get_file_path('argos.csv')
with open(file_path, 'rU') as csvfile:
reader = csv.reader(csvfile)
header = next(reader)
data = defaultdict(lambda:[header])
_ = data[header[5][:5]]
for row in reader:
data[row[5][:5]].append(row)
for file_name, rows in data.items():
with open(file_name, 'w+') as f:
for row in rows:
f.write(str(row) + '\n')