"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
import re

logging.basicConfig(filename='status_log.txt', level=logging.INFO)

fsd_list = [
    ["hki", "cjp_itmu_6@hkfsd.gov.hk"],
    ["kw", ""],
    ["ke", ""],
    ["nt", ""],
]


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"./sent/{filename}"):
                logging.info(
                    f'{datetime.datetime.now()}: The {filename} already exists in ./sent, so skipping.')
            else:
                # shutil.move(filename, './unzip')
                # logging.info(
                #     f'{datetime.datetime.now()}: The {filename} been moved into ./unzip')
                unzip_all_files()

def unzip_all_files():
    os.makedirs('./unzipped', exist_ok=True)
    # zip_files = glob.glob('./unzip/*.zip')
    zip_files = glob.glob('./*.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 "./" directory')
        return

def find_fsd(xml_file):
    tree = ET.parse(xml_file)
    root = tree.getroot()
    fsd_element = root.find('.//districtt')
    if fsd_element is not None:
        return fsd_element.text
    return None


def send_email(fsd_email, pdf_file, folder):
    email_address = 'efax_admin@hkfsd.hksarg'
    email_app_password = ''
    msg = EmailMessage()
    msg['Subject'] = 'Report of Completion of Work for Licensed Premises 持牌處所竣工通知書 (參考編號：' + re.split('_',str(folder),5)[5]+')'
    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('10.18.11.64') as smtp:
        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_files = glob.glob(
            f'./unzipped/{folder}/convertedData/*.xml')
        if not xml_files:
            logging.error(
                f'{datetime.datetime.now()}: No XML files found in {folder}. Skipping.')
            continue
        xml_file = xml_files[0]
        fsd_in_xml = find_fsd(xml_file)

        if fsd_in_xml:
            for fsd, fsd_email in fsd_list:
                if fsd == fsd_in_xml:
                    pdf_files = glob.glob(
                        f'./unzipped/{folder}/convertedData/*.pdf')
                    if not pdf_files:
                        status = 'PDF not found in folder'
                        logging.error(
                            f'{datetime.datetime.now()}: {folder}: {status}')
                        break
                    pdf_file = pdf_files[0]
                    send_email(fsd_email, pdf_file,folder)
                    status = 'Success, Email sent'
                    os.makedirs('./sent', exist_ok=True)
                    shutil.move(f'./unzipped/{folder}', './sent')
                    break
                else:
                    status = 'FSD unit not found in list'
            else:
                status = 'XML not found'
        logging.info(f'{datetime.datetime.now()}: {folder}: {status}')



if __name__ == "__main__":
    main()"improve this code that if found nothing in folder then write a message in log