title: Why does my Lambda function write an empty csv file to S3?
body: <p>I'm calling the YouTube API to download and store channel statistics in S3. I can write a csv file to my S3 bucket without any errors, but it's empty. I have checked this thread <a href="https://stackoverflow.com/questions/61526474/why-the-csv-file-in-s3-is-empty-after-loading-from-lambda">Why the csv file in S3 is empty after loading from Lambda</a>, but I'm not using a <code>with</code> block in <code>to_csv_channel()</code>.</p>
  <p>I'm currently running the script locally with Windows task scheduler with a slightly modified <code>to_csv_channel()</code> method that appends data to an existing csv, but in this case I would like to write a new csv file to S3 every day.</p>
  <pre><code>import json
  import requests
  from datetime import date
  from datetime import datetime
  import csv
  import boto3
  
  class YTstats:
  
      def __init__(self, api_key, channel_id):
          self.api_key = api_key
          self.channel_id = channel_id
          self.channel_name = None
          self.channel_statistics = None
  
      def get_channel_title(self):
          &quot;&quot;&quot;Get the channel name.&quot;&quot;&quot;
  
          url_title = f'https://www.googleapis.com/youtube/v3/channels?part=snippet&amp;id={self.channel_id}&amp;key={self.api_key}'
  
          json_url_title = requests.get(url_title)
          channel_name_json = json.loads(json_url_title.text)
          channel_name_json = channel_name_json['items'][0]['snippet']['title']
  
          self.channel_name = channel_name_json
          return channel_name_json
  
      def get_channel_statistics(self):
          &quot;&quot;&quot;Extract channel statistics&quot;&quot;&quot;
  
          url = f'https://www.googleapis.com/youtube/v3/channels?part=statistics&amp;id={self.channel_id}&amp;key={self.api_key}'
  
          json_url = requests.get(url)
          statistics = json.loads(json_url.text)
          statistics = statistics['items'][0]
  
          self.channel_statistics = statistics
          return statistics
  
      def to_csv_channel(self):
          &quot;&quot;&quot;Saves the channel statistics to a csv file.&quot;&quot;&quot;
          
          s3 = boto3.client('s3')
  
          date_col = datetime.now()
          dt = datetime.strftime(date_col, '%Y-%m-%d')
  
          self.get_channel_title()
          channel = self.channel_name
          channel_id = self.channel_statistics['id']
          views = self.channel_statistics['statistics']['viewCount']
      
          temp_csv_file = csv.writer(open('/tmp/youtube.csv', 'w'))
          temp_csv_file.writerow(['channel_id', 'channel', 'views']) # column headers 
          temp_csv_file.writerow({'channel_id': channel_id, 'channel': channel, 'views': views}) # rows
  
          final_file_name='youtube-api/youtube_'+ dt +'.csv'
          
          s3.upload_file('/tmp/youtube.csv', Bucket = 'my-aws-bucket', Key = final_file_name)
  </code></pre>
  <p>My lambda_handler:</p>
  <pre><code>from youtube import YTstats
  
  def lambda_handler(event, context):
  
      key = 'my_youtube_api_key'
  
      # Mike Shake, Studson Studio
      channel_ids = ['UC6ktP3PLU5sAJxN9Rb0TALg','UC6u6uY4VbvuNtU0BU7F9olw']
  
      for id in channel_ids:
          yt = YTstats(key, id)
          yt.get_channel_statistics()
          yt.to_csv_channel()
  
      return {
          'statusCode': 200
      }
  </code></pre>
  <p>Any help would be appreciated.</p>