title: How to save a tweepy Twitter stream to a file?
body: <p>I have a working script that successfully gathers tweets that mention "stackoverflow". However, I want to run the script in iPython (rather than executive a separate .py file). Ideally, I just want to open it ipyb file, select run all, and let it run for a week or so (not closing my laptop of course) and in result I have a .json file with a week's worth of tweets.</p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>from tweepy import Stream
  from tweepy import OAuthHandler
  from tweepy.streaming import StreamListener
  
  access_token = "x"
  access_token_secret = "x"
  consumer_key = "x"
  consumer_secret = "x"
  
  # file name that you want to open is the second argument
  save_file = open('data.json', 'a')
  
  class listener(StreamListener):
  
      def on_data(self, data):
          print(data)
          return True
  
      def on_error(self, status):
          print(status)
  
  auth = OAuthHandler(consumer_key, consumer_secret)
  auth.set_access_token(access_token, access_token_secret)
  twitterStream = Stream(auth, listener())
  twitterStream.filter(track=["stackoverflow"])
  </code></pre>