Tweepy: can´t print tweets in file
Question: I'm new to python (and programming in general) and I'm trying to write a
script to collect tweets. I can´t get the script to print the tweets in the
file. The tweets appeared in Idle with the error "failed ondata, global name
'saveFile' is not defined" and they don't get printed in the file

i ran this code:

    
    
    from tweepy import Stream
    from tweepy import OAuthHandler
    from tweepy.streaming import StreamListener
    import time
    
    ckey = ???
    csecret = ???
    atoken = ????
    asecret = "???
    
    class listener(StreamListener):
    
    def on_data(self, data):
        try:
            print data
            savefile = open('twitDB.json','a')
            saveFile.write(data)
            savefile.write('\n')
            saveFile.close()
            return True
        except BaseException, e:
            print "failed ondata,",str(e)
            time.sleep(5)
    
    
    def on_error(self, status):
        print status
    
    auth = OAuthHandler(ckey,csecret)
    auth.set_access_token(atoken, asecret)
    twitterStream = Stream(auth, listener())
    twitterStream.filter(track=["movistar"])
    from tweepy import Stream
    from tweepy import OAuthHandler
    from tweepy.streaming import StreamListener
    

Any kind of help is welcomed


Answer: Take a look at the **saveFile** variable.

First time you used it as save**f** ile, then you used save**F** ile (note the
letter **f** and capital **F**)

Change all occurrences to the same name and it should work.

Also, be careful when doing that in the **on_data** method. As far as I know,
it will be called for every single tweet you receive. That is, you will open
the file, write the tweet, close the file and then again for each tweet.

