How to create a file.txt in python if it's object does not exist in an api response? My sample code as below; where for instance, departureGateDelayMinutes is missing from the response. And I want to insert 0 in the file for its absence.
url1 = 'Http://..........'
json_obj1 = urllib2.urlopen(url1)
data1 = json.load(json_obj1)
try:
with open("delays_details.txt",'a') as f:
if (data1['flightStatus']['delays']['departureGateDelayMinutes']):
f.write("Not Found")
else:
f.write(('%d' % item['flightId'])+'|'+str(data1['flightStatus']['delays']['departureGateDelayMinutes'])+'\n')
except:
print "Delay details Not Found! Try again."