title: Saving tweets to JSON file in Python 3.6
body: <p>I'm using the tweepy library to download tweets of certain users. I want to save these tweets into a JSON file but im getting the following error: </p>
  
  <blockquote>
    <p>File "", line 63, in getTweetsList
        json.dump(status._json,file,sort_keys = True,indent = 4)</p>
    
    <p>File "C:\ProgramData\Anaconda3\lib\json__init__.py", line 180, in
    dump</p>
    
    <p>fp.write(chunk)</p>
    
    <p>TypeError: a bytes-like object is required, not 'str'</p>
  </blockquote>
  
  <p>Here is the code:</p>
  
  <pre><code>def getTweetsList(self, screen_name):
      # Twitter only allows access to a users most recent 3240 tweets with this method
      # initialize a list to hold all the tweepy Tweets
      alltweets = []  
  
      # make initial request for most recent tweets (200 is the maximum allowed count)
      new_tweets = self.api.user_timeline(screen_name = screen_name,count=200)
  
      # save most recent tweets
      alltweets.extend(new_tweets)
  
      # save the id of the oldest tweet less one
      oldest = alltweets[-1].id - 1
  
      # keep grabbing tweets until there are no tweets left to grab
      while len(new_tweets) &gt; 0:  
          # all subsiquent requests use the max_id param to prevent duplicates
          new_tweets = self.api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
  
          # save most recent tweets
          alltweets.extend(new_tweets)
  
          # update the id of the oldest tweet less one
          oldest = alltweets[-1].id - 1
  
          print("...%s tweets downloaded so far" % (len(alltweets)))
  
      print("Total tweets downloaded %s" % (len(alltweets)))
      file = open('tweet.json', 'wb') 
      print("Writing tweet objects to JSON please wait...")
      for status in alltweets:
          json.dump(status._json,file,sort_keys = True,indent = 4)
      return alltweets
  </code></pre>
  
  <p>I looked everywhere for an answer, but none of the solutions worked for me. I think it might have something to do with Python 3.6.</p>