I have a folder path and a dictionary. My goal is to pull a value from my dictionary and use it while iterating through the folders. The value will be called fip
:
import arcpy, sys, os, requests, json, xlrd, xlwt
#paths
path = "T:/CCSI/TECH/FEMA/Datasets/NFHL/NFHL_06122018"
prelim_path = "T:/CCSI/TECH/FEMA/Datasets/PreliminaryDFIRMS/Ravi-MSC/Prelim_05092018/Extracted"
#dictionary
masterdict = {'13': ['13245', '13027'], '06': ['06037']}
Everything works fine except when the fip
value is found in the filename. However, if the fip
is not found in the dir
/filename, I still want the word "None" written to that line ('rr' value) of the spreadsheet. I tried using the below code at the same level as the if(fip in dir and '_A_' not in dir and '_B_' not in dir and '_C_' not in dir):' statement by adding an 'elif():' statement, but it essentially writes "None" for EVERY file that does not equal if statement criteria, sets the
rr` value to plus 1 and does that for every dirname and filename in that folder. I just want it to write "None" once if the 'fip' value does not exist in that directory. That's it. Any ideas on to resolve this iteration issue?
rr = 1 #counter
for k, v in masterdict.items():
stateFips = k
print(stateFips)
fipsList = v
print(fipsList)
for fip in fipsList:
for root, dirs, filename in os.walk(prelim_path):
for dir in dirs:
if(fip in dir and '_A_' not in dir and '_B_' not in dir and '_C_' not in dir):
print(dir)
folder = os.path.join(root, dir)
files = os.listdir(folder)
for file in files:
# Create temp variable for cases
if('S_FIRM_Pan' in file and file[-4:] == '.shp' or 'S_FIRM_PAN' in file and file[-4:] == '.shp'):
prelim = os.path.join(folder, file)
# get data for Prelim_sx_firmpan dictionary
suffix = [row[0] for row in arcpy.da.SearchCursor(prelim, "SUFFIX")]
raw_firm_pan = [row[0] for row in arcpy.da.SearchCursor(prelim, "FIRM_PAN")]
Prelim_sx_firmpan = dict(zip(raw_firm_pan, suffix))
P = str(len(Prelim_sx_firmpan))
print("Initial Prelim_sx_firm count for : {} :".format(fip) + P)
# get data for dates
raw_dates = [row[0] for row in arcpy.da.SearchCursor(prelim, "PRE_DATE")]
pre_dates = [d for d in raw_dates if d != datetime.datetime(9999, 9, 9, 0, 0)
if pre_dates == [None]:
pre_date_1 = "None"
sheet1.write(rr, predateCol, pre_date_1)
rr = rr + 1
print("Predate is: ")
print(pre_date_1)
elif pre_dates == []:
pre_date_2 = "None"
sheet1.write(rr, predateCol, pre_date_2)
rr = rr + 1
print("Predate is: ")
print(pre_date_2)
else:
pre_date_3 = max(pre_dates)
pre_date_3 = pre_date_3.strftime('%m/%d/%y')
sheet1.write(rr, predateCol, pre_date_3)
print(rr)
rr = rr + 1
print("Predate is: ")
print(pre_date_3)
elif(fip not in dir):
pre_date_4 = "None"
sheet1.write(rr, predateCol, pre_date_4)
print(rr)
rr = rr + 1
Aucun commentaire:
Enregistrer un commentaire