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")
2
  • Try moving client.upload_file('/tmp/sns_file.csv',BUCKET_NAME,final_file_name) outside of the with block Commented Apr 30, 2020 at 15:09
  • Please correct the formatting of your post. Commented Apr 30, 2020 at 15:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.