3/5/2013 - 2:39 PM

parse csv file and create folders by name

parse csv file and create folders by name

#create folders from csv file
import os
from shutil import move
import csv

folderlist = []
folders = ['0-9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
filename = 'hersteller_alphabetisch.csv'
path = 'logos_test/'
with open(filename, 'rb') as csvfile:
    data = csv.reader(csvfile, delimiter=',')
    for row in data:
        delim = '__'
        pt1 = row[1]  # this is the name
        pt2 = row[0]  # this is the ID
        pt3 = row[3]  # this is the short name
        if not pt1:
            pt1 = '000-miss-name'
        if not pt2:
            pt2 = 'miss-id'
        if not pt3:
            pt3 = 'miss-short'

        foldername = pt1 + delim + pt2 + delim + pt3
#        print foldername

# print(folderlist)
for folder in folderlist:
    firstchar = folder[:1].upper()
    for c in folders:
        if c == firstchar:
            if not os.path.exists(path + folder):
                os.makedirs(path + c + '/' + folder)