Im working on a code that loops through a folder break up the file names in it into specific parts and then reads off sertain parts of the broken name and writes it to a csv sile. The files ser formated as follows test_PAQT_B2H.csv, test_PAQT_B4.csv, and test_PINI_B1H.csv. when it jsut has one file type like just AQT files it works fine but when there is INI files in it when it writes to the csv file for results it writes the ini file multiple times messing up the readability of the data. I jsut want to know how to get it to read each file only once.
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):
filepresent = os.path.isfile(args.fileOutput+'/filecount_'+DT+'.csv')
newrow={'Date':'', 'Total Files':'', 'Total Beds':'', 'Total SQFT':'', 'AQT Files':'', 'INI Files':'','AQT Beds':'','INI Beds':'','AQT Total SQFT':'','INI Total SQFT':'', 'AQT Half Beds':'','INI Half Beds':''}
new_field_names = newrow.keys()
filecount = {}
bedcount = {}
halfbedcount = {}
sqftFactor = {"AQT":64, "INI":50, "n/a":10}
for filename in os.listdir(pathname):
print filename
Extbreak = re.split('[.]', filename)[0]
Printbreak = re.split('_p', Extbreak, flags=re.I)[1]
Typebreak = re.split('_b', Printbreak, flags=re.I)[0]
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]
#print int(Numbreak)*.5
else:
Numbreak = re.split('h', Bedbreak, flags=re.I)[0]
#print Numbreak
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)*.5
bedcount[Typebreak] = bedcount[Typebreak] + int(Numbreak)
for type in filecount:
print dt, type, str(filecount[type]), str(bedcount[type] - halfbedcount[type]), str(sqftFactor[type] * bedcount[type]-(sqftFactor[type]*halfbedcount[type]))
with open(args.fileOutput+'/filecount.csv','ab') as f:
data = [filename]
writer = csv.writer(f)
for item in data:
writer.writerow(data)
data = [dt]
writer = csv.writer(f)
for item in data:
writer.writerow(data)
data = [type]
writer = csv.writer(f)
for item in data:
writer.writerow(data)
data = [type+" files: "+str(filecount[type])]
writer = csv.writer(f)
for item in data:
writer.writerow(data)
data = [type+" bed count: "+str(bedcount[type] - halfbedcount[type])]
writer = csv.writer(f)
for item in data:
writer.writerow(data)
data = [type+" SQFT: "+str(sqftFactor[type] * bedcount[type]-(sqftFactor[type]*halfbedcount[type]))]
writer = csv.writer(f)
for item in data:
writer.writerow(data)
fileBreak(filestart)