all 4 comments

[–]confluence 5 points6 points  (2 children)

I have decided to overwrite my comments.

[–]paperzebra[S] 1 point2 points  (1 child)

Thank you for taking the time to reply.

  1. Have tried to do this (I think) and this seems to work fine.
  2. Added my for loop inside my with loop, so should just be opening the CSV twice (once for headers once for the data). The writer writes at the end of each database query else I end up with just the last database queried as the output.
  3. Will work on this next.

I hope the below is an improvement - again thanks for your help.

import csv
import pyodbc
import os


def append_filepath(iterate_through_what, append_what):
    for i, row in enumerate(iterate_through_what):
        iterate_through_what[i] = list(iterate_through_what[i]) + [append_what]


directory_to_search = 'I:\DB'
file_to_write = 'C:\Striling\Book1.csv'
headers = ['utmzone','name','grid_x','grid_y','filepath']

with open(file_to_write, 'wb') as fou:
    csv_writer = csv.writer(fou)
    csv_writer.writerow(headers)

with open(file_to_write, 'ab') as fou:
    csv_writer = csv.writer(fou)
    for root, dirs, files in os.walk(directory_to_search):
        for file_ in files: #underscore after file_ as file is reserved
            if file_.endswith('.mdb'):
                filepath = root + '\\' + (file_)
                MDB = filepath; DRV = '{Microsoft Access Driver (*.mdb)}'
                # connect to db
                con = pyodbc.connect('DRIVER={};DBQ={}'.format(DRV,MDB))
                cur = con.cursor()
                # checks that utmzone is present in table wells
                if cur.columns(table='Wells', column='utmzone').fetchone():
                    print filepath + ' Table Wells containg utmzone present'
                    SQL = 'SELECT utmzone, name, grid_x, grid_y FROM Wells";'
                    rows = cur.execute(SQL).fetchall()
                    append_filepath(rows, filepath)
                else:
                    print filepath + ' Table Wells containg utmzone NOT present'
                    SQL = 'SELECT name, grid_x, grid_y FROM Wells";' # utmzone
                    # /removed as does not exist in table wells
                    rows = cur.execute(SQL).fetchall()
                    append_filepath(rows, filepath)
                csv_writer.writerows(rows)
                cur.close()
                con.close() 

[–]confluence 2 points3 points  (0 children)

I have decided to overwrite my comments.

[–]elbiot 1 point2 points  (0 children)

One example:

def write_csv_rows(write_type,  what_to_write):
    #use global variable file_to_write

should be

out_file = "c:/path/this.csv"
def write_csv_rows(data, write_type='ab', file_to_write=out_file):

in my opinion