I have a python code that reads a specific folder for specific files. Once it finds a file that matches the re.split peramiters that it is given it then writes the data to a csv file. The issue is that for every file that it loops through and writes it will write everything multiple itmes when im only wantinf it to write things once. Can anyone help me figure out what im doing wrong.

import datetime,glob,os,csv,fnmatch,StringIO,smtplib,argparse,math,re

parser = argparse.ArgumentParser(description='Search art folders.')
parser.add_argument('-b', help='The base path', required=False, dest='basePath', metavar='Base directory path',default='/home/hatterx/Desktop/beds')
parser.add_argument('-o', help='File Output Location', required=False, dest ='fileOutput', metavar='File Output', default='/home/hatterx/Desktop/bedsused')
args = parser.parse_args()

filestart=args.basePath
outputCount= args.fileOutput
DT = datetime.datetime.now().strftime("%Y_%m_%d")
dt = datetime.datetime.now().strftime("%Y/%m/%d %I:%M:%S%p")

def fileBreak(pathname):
    filecount = {}
    bedcount = {}
    halfbedcount = {}
    sqftFactor = {"AQT":64, "INI":50, "n/a":10}

    for filename in os.listdir(pathname):
        Extbreak = re.split('[.]', filename)[0]
        Printbreak = re.split('_p', Extbreak, flags=re.I)[1]
        Typebreak = re.split('_b', Printbreak, flags=re.I)[0]
        if Typebreak not in sqftFactor:
            sqftFactor[Typebreak]=sqftFactor["n/a"]
        Bedbreak = re.split('_b', Extbreak, flags=re.I)[1]
        Halfsearch = re.search('h', Bedbreak, flags=re.I)
        if Halfsearch:
            Numbreak = re.split('h', Bedbreak, flags=re.I)[0]

        else:
            Numbreak = Bedbreak

        if Typebreak not in filecount:
            filecount[Typebreak] = 0

        if Typebreak not in bedcount:
            bedcount[Typebreak] = 0

        if Typebreak not in halfbedcount:
            halfbedcount[Typebreak] = 0

        filecount[Typebreak] = filecount[Typebreak]+1
        if Halfsearch:
            halfbedcount[Typebreak] = halfbedcount[Typebreak] + int(Numbreak)
        else:
            bedcount[Typebreak] = bedcount[Typebreak] + int(Numbreak)

        with open(args.fileOutput+'/Filecount.csv','wb') as f:
            data=['Printer Type', 'File Count']
            writer = csv.writer(f)
            for item in data:
                writer.writerow(data)
            for type in filecount:
                data = [type,str(filecount[type])]
                writer = csv.writer(f)
                for item in data:
                    writer.writerow(data)

        with open(args.fileOutput+'/Bedcount.csv','wb') as f:
            data=['Printer Type','Total Beds','Half Beds','Full Beds']
            writer = csv.writer(f)
            for item in data:
                writer.writerow(data)
            for type in filecount:
                data =[type,str(bedcount[type]+halfbedcount[type]*0.5),str(halfbedcount[type]),str(bedcount[type])]
                writer = csv.writer(f)
                for item in data:
                    writer.writerow(data)

        with open(args.fileOutput+'/SQFTcount.csv','wb') as f:
            data=['Printer Type','Total SQFT','Half Bed SQFT','Full Bed SQFT']
            writer = csv.writer(f)
            for item in data:
                writer.writerow(data)
            for type in filecount:
                data =[type,str(sqftFactor[type] * bedcount[type]+(sqftFactor[type]*halfbedcount[type]*.5)),str(sqftFactor[type]*halfbedcount[type]*.5),str(sqftFactor[type] * bedcount[type])]
                writer = csv.writer(f)
                for item in data:
                    writer.writerow(data)

        with open(args.fileOutput+'/FullInfo.csv','wb') as f:
            data=['Date','Printer Type','Total Beds','Total SQFT']
            writer = csv.writer(f)
            for item in data:
                writer.writerow(data)
            for type in filecount:
                data = [dt,type,str(filecount[type]),str(bedcount[type] + halfbedcount[type]*0.5),str(sqftFactor[type] * bedcount[type]+(sqftFactor[type]*halfbedcount[type]*.5))]
                writer = csv.writer(f)
                for item in data:
                    writer.writerow(data)

fileBreak(filestart)
This question has already been answered. Start a new discussion instead.