Automating an status email from a list of files in a file location. Getting a parser error after updating a list of recipients. Nothing else changed

Viewed 17

Good Afternoon,

I had created a script to automate the collection of a .CSV report, providing some roll-up statistics, and then sending the report with statistics into an email, and sending the email to a set email list.

The recipient asked for an update to the recipient list, and now I receive a parser error. I've done some troubleshooting, and I think I've narrowed it down to the lambda function I'm using to sort the file directory. I'm not quite sure what I'm doing wrong.

Here is my code below:

import pandas as pd
from pathlib import Path
import os
from datetime import date
import win32com.client

#Define today's date for future use
today = str(date.today())

#Define the network directory of the transmission case reports (as a raw string)
dir_name = r'filepath\Daily Transmission Reports\2022'

# Get list of all files only in the given directory
# Sort list of files based on last modification time in ascending order

list_of_files = [entry.path for entry in os.scandir(dir_name) if entry.is_file()]

list_of_files_sorted = sorted(list_of_files, key= lambda x: os.path.getmtime(os.path.join(dir_name,x)))

#Select the last element of list_of_files

CSV_FILE_PATH = list_of_files_sorted[-1]

# Load case data into dataframe
df = pd.read_csv(CSV_FILE_PATH, engine='pyarrow')

# Get unique values from any particular column
column_name = "Status"
unique_value = df.groupby([column_name])[column_name].count()

#Tally the sum of all unique status from a column
total_cases = 'Grand Total ' + str(sum(unique_value))

# email addresses
to_list = 'recipient1@recipient.com; recipient2@recipient.com; recipient3@recipient.com; recipient4@recipient.com; recipient5@recipient.com; recipient6@recipient.com; recipient7@recipient.com; recipient8@recipient.com; recipient9@recipient.com; recipient10@recipient.com; recipient11@recipient.com; recipient12@recipient.com; recipient13@recipient.com; recipient14@recipient.com; recipient15@recipient.com'
cc_list = 'recipient16@recipient.com; recipient17@recipient.com; recipient18@recipient.com; recipient19@recipient.com'

# Create an email, fill out the "To", "CC", "Subject" and email body, and attach the appropriate *.csv file
outlook = win32com.client.Dispatch("Outlook.Application")
mail = outlook.CreateItem(0)
mail.To = to_list
mail.CC = cc_list
mail.Subject = today + " Transmission Case Report"
mail.HTMLBody = f"""
                    Good Morning,<br><br>
                    Today’s case report is attached.<br><br>
                    Dealer Action Required {unique_value["Dealer Action Required"]}<br>
                    In Progress {unique_value["In Progress"]}<br>
                    New {unique_value["New"]}<br>
                    Waiting on Parts {unique_value["Waiting on Parts"]}<br>
                    {total_cases}<br><br>
                    Thanks,<br>
Me │ Supervisor | Customer Service │ Company <br>
Address │ City, State ZIP │ recipient19@recipient.com"""

attachment_path = str(CSV_FILE_PATH)
mail.Attachments.Add(Source=attachment_path)
mail.Display()
#mail.Send()

The error I'm receiving in cmd is:

C:\Windows>"C:\Users\Me\AppData\Local\Programs\Python\Python310\python.exe" "filepath\Daily Transmission Reports\2022\Daily_Transmission_Report.py"
Traceback (most recent call last):
  File "filepath\Daily Transmission Reports\2022\Daily_Transmission_Report.py", line 25, in <module>
    df = pd.read_csv(CSV_FILE_PATH, engine='pyarrow')
  File "C:\Users\Me\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 678, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 581, in _read
    return parser.read(nrows)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1246, in read
    df = self._engine.read()
  File "C:\Users\Me\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\arrow_parser_wrapper.py", line 149, in read
    table = pyarrow_csv.read_csv(
  File "pyarrow\_csv.pyx", line 1217, in pyarrow._csv.read_csv
  File "pyarrow\_csv.pyx", line 1226, in pyarrow._csv.read_csv
  File "pyarrow\error.pxi", line 144, in pyarrow.lib.pyarrow_internal_check_status
  File "pyarrow\error.pxi", line 100, in pyarrow.lib.check_status
pyarrow.lib.ArrowInvalid: CSV parse error: Expected 1 columns, got 3: list_of_files_sorted = sorted(list_of_files, key= lambda x: os.path.getmtime(os.path.join(dir_na ...

The .CSV report is generated on a schedule by Salesforce. As far as I can tell, the data itself in the .csv hasn't changed, there are no hidden characters that I can tell that would throw off my dataframe.

For some reason, the lambda function is being read as three columns, as far as I can tell. The only thing I did last when this was still working was changed the recipient list. Removed two recipients and added three.

During troubleshooting I updated pip and pandas, both were one version out of date. Still the same error. Installed pyarrow and got some more helpful debugging.

Your help is appreciated. I'm hoping it is something dumb that I overlooked!

0 Answers
Related