0
import os
import csv
import boto3

client = boto3.client('s3')

fields = ['dt','dh','key','value']
row = [dt,dh,key,value]
print(row)

# name of csv file 
filename = "/tmp/sns_file.csv"

# writing to csv file 
with open(filename, 'a',newline='') as csvfile: 
    # creating a csv writer object 
    csvwriter = csv.writer(csvfile)

    # writing the fields 
    csvwriter.writerow(fields) 

    # writing the data row
    csvwriter.writerow(row)
    final_file_name="final_report_"+dt+".csv"
    client.upload_file('/tmp/sns_file.csv',BUCKET_NAME,final_file_name)
    if os.path.exists('/tmp/sns_file.csv'):
        os.remove('/tmp/sns_file.csv')
    else:
        print("The file does not exist")
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

1 Answers1

1

Python's with block is a context manager, which means, in simple terms, it will "clean up" after all operations within it are done.

In context of files "clean up" means closing file. Any changes you write to the file will not be saved on disk until you close the file. So you need to move upload operation outside and after the with block.

Oleksii Donoha
  • 2,911
  • 10
  • 22