Python Script to ESRI Shapefile

"""This Python script looks for a folder that contains the Code-Point Open zip file 
and then writes all the individual CSV files contained within the zip file and writes 
to a new CSV file, CodePoint_Open.csv. The script then uses OGR2OGR to translate the CSV
file to an ESRI Shapefile using a VRT file which specifies the XY columns for the coordinates
"""
 
##Licence below
"""THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
OF THE POSSIBILITY OF SUCH DAMAGE"""
 
 
 
#Import the relevant modules
 
 
import os
import csv
import sys
import zipfile
import time
from time import strftime
import subprocess
import StringIO
 
 
 
 
def main():
	starttime = time.time()
	fileList = []
	count = 0
	print 'Please type in the full path to the directory of Code-Point Open file:'
	directorypath = raw_input('Directory Path: ')
 
	if os.path.isdir(directorypath):
		print 'That directory path exists, program will continue'
		#os.chdir(directorypath)
 
		for dirname, dirnames, filenames in os.walk(directorypath):
			for filename in filenames:
				if filename.endswith(".zip"):
					fullname = os.path.join(directorypath, filename)
					fileList.append(fullname)
					count = count +1
					
		print 'Program has found %s zip files' %count
 
		print 'Program will now read from the Zip file and write to a new CSV CodePt_Open.csv'
 
		headerline = ["Postcode","Positional_quality_indicator","Eastings","Northings","Country_code","NHS_regional_HA_code","NHS_HA_code","Admin_county_code","Admin_district_code","Admin_ward_code"]
 
		if os.path.isfile('CodePt_Open.csv'):
			os.remove('CodePt_Open.csv')
			codeptcsv = open('CodePt_Open.csv', 'a')
			codept_writer = csv.writer(codeptcsv, delimiter=',', lineterminator='\n')
			codept_writer.writerow(headerline)
		else:
			codeptcsv = open('CodePt_Open.csv', 'a')
			codept_writer = csv.writer(codeptcsv, delimiter=',', lineterminator='\n')
			codept_writer.writerow(headerline)
 
		fileList2 = []
		for zipname in fileList:
			zname = open(zipname, 'rb')
			zfile = zipfile.ZipFile(zname)
			for name in zfile.namelist():
				if name.startswith('Data/CSV/'):
					if name.endswith(".csv"):
						fileList2.append(name)
 
		for csvzipname in fileList2:
			data = StringIO.StringIO(zfile.read(csvzipname))
			reader = csv.reader(data, delimiter=',', doublequote=False, lineterminator= '\n', quotechar= '"', quoting= 0, skipinitialspace=True)
			for row in reader:
				codept_writer.writerow(row)
 
		print 'Program has finished creating the new CSV CodePt_Open.csv'
 
		print 'Program will now translate CodePt_Open.csv to ESRI Shapefile using OGR2OGR'
 
		print 'ESRI Shapefiles can only have a certain attribute header length so some may be cut short'
 
		shapefile_cmd = ("""ogr2ogr -f "ESRI Shapefile" CodePt_Open.shp codept_open.vrt""" )
		run = subprocess.Popen(shapefile_cmd, shell=True)
		run.communicate()
 
		print 'Program has finished creating CodePt_Open.shp'
 
		endtime = time.time()
    	elapsed = endtime - starttime
    	print 'Finished translating data at ', strftime("%a, %d %b %Y %H:%M:%S")
    	print 'Elapsed time: ', round(elapsed/60,1), ' minutes'
    	print 'Program will now close'
    	time.sleep(5)
    	sys.exit()
 
 
if __name__ == '__main__':
	main()