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")
Asked
Active
Viewed 470 times
0

John Rotenstein
- 241,921
- 22
- 380
- 470
-
Try moving `client.upload_file('/tmp/sns_file.csv',BUCKET_NAME,final_file_name)` outside of the `with` block – jellycsc Apr 30 '20 at 15:09
-
Please correct the formatting of your post. – jarmod Apr 30 '20 at 15:17
1 Answers
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