please help to rewrite the code "import zipfile import shutil import os import re from…
- User
please help to rewrite the code "import zipfile import shutil import os import re from functions.sendMail import sendMail, sendErrorMail from functions.createLogger import createLogger
sourceDir = "." unzipDir = "unzip/convertedData" regexZip = re.compile('(.*zip)') logger = createLogger() os.makedirs('pdf')
try: for file in os.listdir(sourceDir): if os.path.isfile(os.path.join(sourceDir, file)): if regexZip.match(file): with zipfile.ZipFile(file, 'r') as zip_ref: zip_ref.extractall("unzip") if os.path.exists(f'sent/{file}'): logger.warning(f'{file} Replaced') shutil.copy(file, "sent/") os.remove(file)
if os.path.exists(unzipDir): for file in os.listdir(unzipDir): if os.path.isfile(os.path.join(unzipDir, file)): if regexPdf.match(file): if os.path.exists(f'pdf/{file}'): logger.warning(f'{file} Replaced') shutil.copy(f"unzip/convertedData/{file}", "pdf/") sendMail(file) logger.info(f'{file} Mail sent') else: logger.info('No submission found') if os.path.exists('pdf'): shutil.rmtree('pdf') if os.path.exists('unzip'): shutil.rmtree('unzip')except Exception as e: if os.path.exists('pdf'): shutil.rmtree('pdf') if os.path.exists('unzip'): shutil.rmtree('unzip') sendErrorMail(e) logger.error(e)"
- Assistant
import zipfile import shutil import os import re from functions.sendMail import sendMail, sendErrorMail from functions.createLogger import createLogger
sourceDir = "." unzipDir = "unzip/convertedData" regexZip = re.compile('(.*zip)') logger = createLogger() os.makedirs('pdf')
try: for file in os.listdir(sourceDir): if os.path.isfile(os.path.join(sourceDir, file)) and regexZip.match(file): with zipfile.ZipFile(file, 'r') as zip_ref: zip_ref.extractall("unzip") if os.path.exists(f'sent/{file}'): logger.warning(f'{file} Replaced') shutil.copy(file, "sent/") os.remove(file)
if os.path.exists(unzipDir): for file in os.listdir(unzipDir): if os.path.isfile(os.path.join(unzipDir, file)) and regexPdf.match(file): if os.path.exists(f'pdf/{file}'): logger.warning(f'{file} Replaced') shutil.copy(f"unzip/convertedData/{file}", "pdf/") sendMail(file) logger.info(f'{file} Mail sent') else: logger.info('No submission found') if os.path.exists('pdf'): shutil.rmtree('pdf') if os.path.exists('unzip'): shutil.rmtree('unzip')except Exception as e: if os.path.exists('pdf'): shutil.rmtree('pdf') if os.path.exists('unzip'): shutil.rmtree('unzip') sendErrorMail(e) logger.error(e)