rewrite this code"
import zipfile
import shutil
import os
import re
from functions.sendMail import sendMail, sendErrorMail
from functions.createLogger import createLogger
import zipfile
import shutil
import os
import re
from functions.sendMail import sendMail, sendErrorMail
from functions.createLogger import createLogger
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)
"
)′)regexPdf=re.compile(′(.∗pdf
os.makedirs('pdf', exist_ok=True)
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')
shutil.rmtree('pdf', ignore_errors=True)
shutil.rmtree('unzip', ignore_errors=True)
except Exception as e:
shutil.rmtree('pdf', ignore_errors=True)
shutil.rmtree('unzip', ignore_errors=True)
sendErrorMail(e)
logger.error(e)