1

I am currently trying to send database query results in an Excel sheet attaching it to a mail. It works fine when I am doing it locally and I even get the mail with query results as Excel attachment however I get the following error after deploying and triggering through Lambda.

[Errno 30] Read-only file system: 'default.xlsx'

This is my code

def write_to_default_ws_row_col(self,reports):
    wb = Workbook(write_only=True)
    report_ws = wb.create_sheet("Reports")
    # write header
    report_ws.append(["File Name", "CSG Sent Count", "Rject Count", "CSG Process Count", "File CSG Mailitem Count", "File Status", "File Create Date"])

    # write data
    for report in reports:
        report_FILE_NAME = report[0]
        report_FILE_ITEM_COUNT = report[1]
        report_REJECT_COUNT = report[2]
        report_HSDC_TOTDOC = report[3]
        report_FILE_CSG_MAILITEM_COUNT = report[4]
        report_FILE_STATUS = report[5]
        report_FILE_CREATE_DATE = report[6]
        report_ws.append([report_FILE_NAME, report_FILE_ITEM_COUNT, report_REJECT_COUNT, report_HSDC_TOTDOC, report_FILE_CSG_MAILITEM_COUNT, report_FILE_STATUS, report_FILE_CREATE_DATE])


    wb.save('tmp/default.xlsx')
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • Is that the entire error message? What do/don’t you understand from that? – AMC Mar 19 '20 at 20:57
  • yes that is the entire message. What I understand is the The file is in the wrong location because when I give the path as '/temp/default.xlsx', it says the path or directory does not exist. What am I doing wrong? – nalin sawle Mar 19 '20 at 21:02
  • Does the file already exist? Possibly related: https://stackoverflow.com/questions/39383465/python-read-only-file-system-error-with-s3-and-lambda-when-opening-a-file-for-re. – AMC Mar 19 '20 at 22:45
  • The problem is resolved: I added: os.chdir('/tmp') after the definition of the function def write_to_default_ws_row_col(self,reports). – nalin sawle Mar 19 '20 at 23:48
  • So the code will look like this: def write_to_default_ws_row_col(self,reports) : os.chdir('/tmp') – nalin sawle Mar 19 '20 at 23:49
  • Note: This will not work locally only on lambda – nalin sawle Mar 19 '20 at 23:50
  • Ooh wait a minute, what is the current working directory when you're running the program? Is `'tmp/default.xlsx'` the absolute path for the new XLSX file? – AMC Mar 19 '20 at 23:52

1 Answers1

0

Corrected Code ''' def get_day1_reports(connection): cursor = connection.cursor() query = "select FILE_NAME,FILE_ITEM_COUNT as SENT_TO_CSG_COUNT,REJECT_COUNT,HSDC_TOTDOC as CSG_PROCESS_COUNT,FILE_CSG_MAILITEM_COUNT,FILE_STATUS,FILE_CREATE_DATE from RECON_HS_FILE_TRACKING where datediff(curdate(),substring(FILE_NAME,30,8)) = 2" cursor.execute(query) data = cursor.fetchall() return data

class Email_reports(): def init(self, settings): self.sender = settings.fromEmail self.recipient = settings.toEmail self.aws_region = settings.region self.charset = "UTF-8" context_opts = dict() self.logger = HSLogger(name, **context_opts)

def write_to_default_ws_row_col(self,reports):
    os.chdir('/tmp')
    wb = Workbook(write_only=True)
    report_ws = wb.create_sheet("Reports")
    # write header
    report_ws.append(["File Name", "CSG Sent Count", "Rject Count", "CSG Process Count", "File CSG Mailitem Count", "File Status", "File Create Date"])

    # write data
    for report in reports:
        report_FILE_NAME = report[0]
        report_FILE_ITEM_COUNT = report[1]
        report_REJECT_COUNT = report[2]
        report_HSDC_TOTDOC = report[3]
        report_FILE_CSG_MAILITEM_COUNT = report[4]
        report_FILE_STATUS = report[5]
        report_FILE_CREATE_DATE = report[6]
        report_ws.append([report_FILE_NAME, report_FILE_ITEM_COUNT, report_REJECT_COUNT, report_HSDC_TOTDOC, report_FILE_CSG_MAILITEM_COUNT, report_FILE_STATUS, report_FILE_CREATE_DATE])

    # abspath = os.path.abspath(__file__)
    # logger.info('abspath %s.', abspath)
    # dname = os.path.dirname(abspath)
    # logger.info('dname %s.', dname)
    # os.chdir(dname)
    # dirpath = os.getcwd()
    # logger.info('current dir %s.', dirpath)
    # contents = os.listdir("/tmp")
    # logger.info('before save %s.', contents)
    wb.save(filename = 'day1_reports.xlsx')

'''