0

In my airflow task, I am creating a file using open() method in airflow dag and writing records into it. Then sending it with a mail within same task. Will it get deleted automatically or will exists into the dag?

filename = to_report_name(context)+'_'+currentNextRunTime.strftime('%m.%d.%Y_%H-%M')+'_'+currentNextRunTime.tzname()+'.'+extension.lower()
            with open(filename, "w+b") as file:
                file.write(download_response.content)
                print(file.name)
            send_report(context,file)

1 Answers1

1

The file will not be deleted automaticly. The code you execute is pure Python if you want the file to be deleted once the operation is done then use tempfile module which gurentee the file will be deleted once it's closed. Example:

import tempfile, os
with tempfile.NamedTemporaryFile() as file:
    os.rename(file.name, '/tmp/my_custom_name.txt') # use this if you want to rename the file
    file.write(...)
Elad Kalif
  • 14,110
  • 2
  • 17
  • 49
  • this time i tried this, but getting exception as **FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpimudg_s1'** `filename = to_report_name(context)+'_'+currentNextRunTime.strftime('%m.%d.%Y_%H-%M')+'_'+currentNextRunTime.tzname()+'.'+extension.lower() path='/tmp/' with NamedTemporaryFile() as file: os.rename(os.path.join(path, file.name), os.path.join(path, filename)) file.write(download_response.content) print(file.name) send_report(context,file)` – Kundan Kumar Sep 18 '22 at 15:25
  • refrence to the file only inside the block – Elad Kalif Sep 18 '22 at 15:52