简体   繁体   中英

Writing to a file in python

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()

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM