0

Here is my code so far:

import flickrapi import osmapi import geopy from geopy.geocoders import Nominatim from time import sleep api_key = "xxxxxxxxxxxxxxxxxxxx" secret_api_key = "xxxxxxxx" flickr = flickrapi.FlickrAPI(api_key, secret_api_key) def obtainImages3(): file = open('citylist.txt', 'r') file2 = open('obtainedImages.txt', 'a+') for line in file: fields = line.strip().split() city = fields[1] group_list = flickr.groups.search (api_key=api_key, text = city, per_page = 100) for group in group_list[0]: group_images = flickr.groups.pools.getPhotos (api_key=api_key, group_id = group.attrib['nsid'], has_geo = 1, extras = 'geo, tags, url_q') for image in group_images[0]: try: photo_location = flickr.photos_geo_getLocation(photo_id=image.attrib['id']) lat = float(photo_location[0][0].attrib['latitude']) lon = float(photo_location[0][0].attrib['longitude']) id = str(image.attrib['id']) url = str(image.attrib['url_q']) geolocator = Nominatim() location = geolocator.reverse("{}, {}".format(lat, lon)) dict = location.raw osmid = dict.get('osm_id', 'default_value_if_null_here') osmtype = dict.get('osm_type', 'default_value_if_null_here') osmaddress = dict.get('display_name', 'default_value_if_null_here') sleep(1) if(osmtype == 'node'): print id print url print osmaddress file2.write("%s" % id) file2.write(' ') file2.write("%s" % url) file2.write(' ') file2.write("%s" % lat) file2.write(' ') file2.write("%s" % lon) file2.write(' ') file2.write("%s" % osmaddress) file2.write('\n') except Exception: pass file2.close() file.close() obtainImages3() 

The code is running fine without any errors and my print statements are working fine. However nothing is being written to the file. I have used the same method for writing to a file in a very similar program and it worked perfectly but here it is not working. Can anyone suggest why this might be? Thank you!!

4
  • 2
    Can you reduce this to the smallest complete example that still exhibits the problem? See How to create a Minimal, Complete, and Verifiable example. Commented Feb 10, 2016 at 15:36
  • 1
    Try adding file2.flush() after the file2.wirte('n') Commented Feb 10, 2016 at 15:40
  • 1
    "running without any errors" in this case means "catching and suppressing errors without looking at them" Commented Feb 10, 2016 at 15:40
  • @RobertJacobs thank you youre file2.flush() suggestion worked! Commented Feb 10, 2016 at 15:45

3 Answers 3

3

Adding this so I can get credit for answer that was in comment.

Try adding file2.flush() after the file2.write('\n')

Sign up to request clarification or add additional context in comments.

1 Comment

If this works, then the close() function is either not being called or is not working.
3

I think the file.close() function should go into the function node, because it is in a buffer. or, remove the "except Exception:" method, and see what you got.

Comments

3

Clearly, the file write is throwing an exception. In your code, you are ignoring the exception. Change the except statement to:

except Exception as e: print e 

and you will see the error. It probably cannot open the file.

5 Comments

the only error it prints is 'photo has no location info' which is the error I am passing in the question, it is not giving any file errors
@RyanKilkelly Figure out what type of error it is and only catch that type of error with the except block.
How is it attempting the file writes if it is throwing an exception on location = geolocator.reverse("{}, {}".format(lat, lon))?
A suggestion from above of just adding file2.flush() after writing to the file worked, thanks for your help anyways
Great! Be careful with handling those exceptions though...ignoring exceptions is never good. At least print them out so you remember there is an issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.