2

In the following code I simply want to write the data I obtained from flickr in a neat way in a file for use later. However although the console throws no errors, nothing is being written to my file. Can anyone help me out with this, thank you in advance!

import flickrapi

api_key = "xxxxxxxxxxx"
secret_api_key = "xxxxxxxxxxxxxxxx"
flickr = flickrapi.FlickrAPI(api_key, secret_api_key)

def obtainImages():

    photo_list = flickr.photos.search(api_key=api_key, has_geo=1, per_page = 500)
    file = open("obtainedImages.txt", "w")

    for photo in photo_list[0]:
        photo_location = flickr.photos_geo_getLocation(photo_id=photo.attrib['id'])

        id = str(photo[0].attrib['id'])
        lat = str(photo_location[0][0].attrib['latitude'])
        long = str(photo_location[0][0].attrib['longitude'])

        file.write("%s" %id)
        file.write(' ')
        file.write("%s" % lat)
        file.write(' ')
        file.write("%s" % long)
        file.write('\n')

obtainImages()

Here is updated code, that is again showing no errors but is not writing anything to the file. Lat and lon are printing ok

import flickrapi

api_key = "xxxxxxxxxxxxxxxxx"
secret_api_key = "xxxxxxxxx"
flickr = flickrapi.FlickrAPI(api_key, secret_api_key)

def obtainImages():

    photo_list = flickr.photos.search(api_key=api_key, has_geo=1, per_page = 500)
    file = open("obtainedImages.txt", "w")

    for photo in photo_list[0]:

        photo_location = flickr.photos_geo_getLocation(photo_id=photo.attrib['id'])
        lat = str(photo_location[0][0].attrib['latitude'])
        long = str(photo_location[0][0].attrib['longitude'])

        file.write("%s" % lat)
        file.write(' ')
        file.write("%s" % long)
        file.write('\n')

    file.close()
obtainImages()
RyanKilkelly
  • 279
  • 1
  • 4
  • 15
  • Did you `print photo_list[0]`? – Padraic Cunningham Jan 18 '16 at 14:51
  • 2
    technically, you should call `file.close()` when you are done writing, though some implementations take care of this for you. – gariepy Jan 18 '16 at 14:53
  • I've tried that and I have tried printing lat and long etc out before I attempted to write to the file and it worked fine, it just so happens when I try to write to the file there are no errors but nothing is written into my file – RyanKilkelly Jan 18 '16 at 14:56
  • maybe you should use exceptions such as IOError to see if w=everything is fine – cssGEEK Jan 18 '16 at 14:59
  • I have added in file.close(), and have check the printing again. Lat and long are printing fine however id is not. I removed id and it ran ok again but with no writing, but now having put in file.close(), my error says "line 20, in obtainImages file.write("%s" % lat) ValueError: I/O operation on closed file" – RyanKilkelly Jan 18 '16 at 15:09
  • I have applied all of your suggestions(see updated code above) and am still getting the same problem of no errors but nothing is written to the file, I am stumped! – RyanKilkelly Jan 18 '16 at 15:17
  • Which txt file are you checking if it's written or not? The one that is in the same directory with your `.py` file, right? – Lafexlos Jan 18 '16 at 15:22
  • I mean, where are your `.txt` and `.py` files located? – Lafexlos Jan 18 '16 at 15:32
  • please give a minimal example - is this about writing to a file or about flickr? Often when reducing the code to something simple you will find the problem yourself. – dan-man Jan 18 '16 at 16:03
  • I tested the file-writing part with hard-coded values for id, lat and long, and it worked fine. I would try to print the values you're getting to the console and see if there is a problem with what flickrapi is giving you. – gariepy Jan 18 '16 at 16:10
  • One more comment: `id()` and `long()` are built-in functions in python 2.7, and `id()` is a built-in function in python 3.x, so while python will let you name variables `id` and `long`, it's a very risky practice - so just never do it. :) – gariepy Jan 18 '16 at 16:12

2 Answers2

3

Add file.close() at the end of your obtainImages() method.

def obtainImages():

    photo_list = flickr.photos.search(api_key=api_key, has_geo=1, per_page = 500)
    file = open("obtainedImages.txt", "w")

    for photo in photo_list[0]:
        photo_location = flickr.photos_geo_getLocation(photo_id=photo.attrib['id'])

        id = str(photo[0].attrib['id'])
        lat = str(photo_location[0][0].attrib['latitude'])
        long = str(photo_location[0][0].attrib['longitude'])

        file.write("%s" %id)
        file.write(' ')
        file.write("%s" % lat)
        file.write(' ')
        file.write("%s" % long)
        file.write('\n')
    file.close()  ## add here
gariepy
  • 3,576
  • 6
  • 21
  • 34
  • Alternatively, [use `with`](http://stackoverflow.com/questions/9282967/how-to-open-a-file-using-the-open-with-statement) so you won't forget to close it. – Cees Timmerman Jan 18 '16 at 16:45
1

Please check the following

  1. The file obtainedImages.txt is in the same directory as the python file.
  2. When you run the python file, you are closing & re-opening the .txt file to check for changes
  3. Move your cursor around the file and check if the spaces and newline are getting written, if yes that means there is something wrong with the values

If you still do not get output, try the following debugging steps:

  1. Replace lat & long with static values and see if they are written to file.
  2. Comment out the for loop and check if the values are written to file (Re-indent the for block when you comment the for loop)
  3. It is possible that the file you are writing to is not the one you are reading from. So you could open the file in read mode and try to print its contents using a python script.
S Raghav
  • 1,386
  • 1
  • 16
  • 26