“import os import zipfile import glob import smtplib from email.message import…
- User
“import os import zipfile import glob import smtplib from email.message import EmailMessage import xml.etree.ElementTree as ET import logging import datetime import shutil
logging.basicConfig(filename=‘status_log.txt’, level=logging.INFO)
fsd_list = [ [“aaa”, “aaa@email.com”], [“bbb”, “bbb@email.com”], [“ccc”, “ccc@email.com”], ]
auto create the unzip folder
def move_zip_files(): os.makedirs(‘./unzip’, exist_ok=True)
filenames = os.listdir(‘.’)
for filename in filenames: if filename.endswith(‘.zip’): if os.path.exists(f”./unzip/{filename}“): logging.info( f’{datetime.datetime.now()}: The {filename} already exists in ./unzip, so skipping.‘) else: shutil.move(filename, ‘./unzip’) logging.info( f’{datetime.datetime.now()}: The {filename} been moved into ./unzip’) unzip_all_files()
Function to unzip all files in ./unzip folder
def unzip_all_files(): os.makedirs(‘./unzipped’, exist_ok=True) zip_files = glob.glob(‘./unzip/.zip’) for zip_file in zip_files: with zipfile.ZipFile(zip_file, ‘r’) as zfile: zfile.extractall(‘./unzipped’) logging.info(f’{datetime.datetime.now()}: {zip_file} unzipped’) if not zip_files: logging.error( f’{datetime.datetime.now()}: No ZIP files found in “./unzip” directory’) return
Function to find FSD in an XML file
def find_fsd(xml_file): tree = ET.parse(xml_file) root = tree.getroot() fsd_element = root.find(‘.//unit’) if fsd_element is not None: return fsd_element.text return None
Function to send email with a PDF file attached
def send_email(fsd_email, pdf_file): email_address = ‘<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>’ email_app_password = ‘brwgearofnryfinh’ msg = EmailMessage() msg[‘Subject’] = ‘FSD PDF file’ msg[‘From’] = email_address msg[‘To’] = fsd_email
msg.set_content(‘Please find the PDF file attached.’)
with open(pdf_file, ‘rb’) as pdf: msg.add_attachment(pdf.read(), maintype=‘application’, subtype=‘pdf’, filename=pdf_file)
with smtplib.SMTP_SSL(‘smtp.gmail.com’, 465) as smtp: smtp.login(email_address, email_app_password) smtp.send_message(msg) logging.info( f’{datetime.datetime.now()}: {pdf_file} sent to {fsd_email}‘)
def main(): move_zip_files() unzipped_folders = os.listdir(’./unzipped’)
for folder in unzipped_folders: xml_file = glob.glob(f’./unzipped/{folder}/.xml’)[0] fsd_in_xml = find_fsd(xml_file)
if fsd_in_xml: for fsd, fsd_email in fsd_list:
If FSD in the XML file matches the one in the list, send the email with the PDF attached
if fsd == fsd_in_xml: pdf_file = glob.glob(f’./unzipped/{folder}/.pdf’)[0] send_email(fsd_email, pdf_file) status = ‘Success’ break else: status = ‘FSD not found in list’ else: status = ‘FSD not found in XML’ logging.info(f’{datetime.datetime.now()}: {folder}: {status}')
- Assistant
To achieve this, you can modify the
unzip_all_files()function to create a folder with the name of the zip file and then extract its contents into that folder. Here's an updated code:codedef unzip_all_files(): os.makedirs('./unzipped', exist_ok=True) zip_files = glob.glob('./unzip/*.zip') for zip_file in zip_files: folder_name = os.path.splitext(os.path.basename(zip_file))[0] os.makedirs(f'./unzipped/{folder_name}', exist_ok=True) with zipfile.ZipFile(zip_file, 'r') as zfile: zfile.extractall(f'./unzipped/{folder_name}') logging.info(f'{datetime.datetime.now()}: {zip_file} unzipped') os.remove(zip_file) if not zip_files: logging.error(f'{datetime.datetime.now()}: No ZIP files found in "./unzip" directory')This code creates a folder for each zip file with the same name as the zip file (without the extension), extracts the contents of the zip file into that folder, removes the original zip file, and logs the event.
- User
why need to os.remove(zip_file)
- Assistant
The
os.remove(zip_file)line removes the original zip files after it has been successfully extracted.This is a good practice because after extracting the contents of a zip file, the original zip file becomes redundant and takes up unnecessary storage space.
By removing the zip files, we can keep our file system organized and uncluttered, and prevent the accumulation of unnecessary files.