The code imports the modules zipfile, shutil, os, and re, as well as two custom modules, sendMail and createLogger. It creates a regex pattern for zip and pdf files, sets a source directory and an unzip directory, and creates a logger. It then creates a folder named pdf in the current directory.
The code then tries to loop through each file in the source directory. If the file is a zip file, it extracts its contents to the unzip directory. If the file has already been processed and exists in the sent folder, it logs a warning and copies the file to the sent folder before deleting it from the source directory.
Next, it loops through each file in the unzip directory. If the file is a pdf file, it copies it to the pdf directory, sends an email with the pdf file attached, and logs that the email was sent. If no pdf files were found, it logs that no submissions were found.
Finally, it deletes the pdf and unzip directories. If an exception occurs, it deletes the pdf and unzip directories, sends an error email, and logs the error.
Here's a possible rewrite of the code using functions:
import zipfile
import shutil
import os
import re
from functions.sendMail import sendMail, sendErrorMail
from functions.createLogger import createLogger
def extract_zip_files(source_dir, unzip_dir, regex_zip, logger):
"""
Extracts zip files in the source directory to the unzip directory, logs warnings if files exist in the sent
directory, and moves processed files to the sent directory.
"""
os.makedirs(unzip_dir, exist_ok=True)
sent_dir = 'sent'
os.makedirs(sent_dir, exist_ok=True)
for file in os.listdir(source_dir):
file_path = os.path.join(source_dir, file)
if os.path.isfile(file_path) and regex_zip.match(file):
with zipfile.ZipFile(file_path, 'r') as zip_ref:
zip_ref.extractall(unzip_dir)
sent_file = os.path.join(sent_dir, file)
if os.path.exists(sent_file):
logger.warning(f'{file} Replaced')
shutil.copy(file_path, sent_file)
os.remove(file_path)
def process_pdf_files(unzip_dir, pdf_dir, regex_pdf, logger):
"""
Copies pdf files in the unzip directory to the pdf directory, sends emails with pdf files attached, and logs
information about the emails sent and pdf files processed.
"""
if not os.path.isdir(pdf_dir): os.makedirs(pdf_dir)
for file in os.listdir(unzip_dir):
file_path = os.path.join(unzip_dir, file)
if os.path.isfile(file_path) and regex_pdf.match(file):
pdf_file = os.path.join(pdf_dir, file)
if os.path.exists(pdf_file):
logger.warning(f'{file} Replaced')
shutil.copy(file_path, pdf_file)
sendMail(pdf_file)
logger.info(f'{file} Mail sent')
if not os.listdir(pdf_dir):
logger.info('No submission found')
def main():
source_dir = '.'
unzip_dir = 'unzip/convertedData'
regex_zip = re.compile(r'(.*\.zip)')
regex_pdf = re.compile(r'(.*\.pdf)')
logger = createLogger()
try:
extract_zip_files(source_dir, unzip_dir, regex_zip, logger)
process_pdf_files(unzip_dir, 'pdf', regex_pdf, logger)
except Exception as e:
if os.path.isdir('pdf'): shutil.rmtree('pdf')
if os.path.isdir(unzip_dir): shutil.rmtree(unzip_dir)
sendErrorMail(e)
logger.error(e)
else:
if os.path.isdir('pdf'): shutil.rmtree('pdf')
if os.path.isdir(unzip_dir): shutil.rmtree(unzip_dir)
if __name__ == '__main__':
main()
The main code is wrapped in a main function. The extract_zip_files and process_pdf_files functions encapsulate the functionality for dealing with zip and pdf files, respectively.
The os.makedirs function is used to create directories unzip/convertedData, sent, and pdf. If the directories already exist, it does not raise an error.
The os.path.join function is used to create file paths by joining the directory path and the file name. The os.path.isfile function is used to check if a file path exists and refers to a file.
The shutil.copy function is used to copy files from source folders to destination folders.
The try and except keywords are used to catch and handle exceptions.
Notice that the else keyword is used to execute code if no exceptions are raised in the try block.
Lastly, the __name__ == "__main__" statement is included to prevent the code from executing if the module is imported rather than run directly.