Home > Software engineering >  Exiting a loop in my python function when a specific value is empty
Exiting a loop in my python function when a specific value is empty

Time:03-09

I am having a problem with needing to skip this iteration when the variable queue is blank/empty. What I am trying to do is is end the function so that the loop that is using the function process_zip_file can move on to the next file. What happens is I open a csv file and look at the header rows to build out a directory path. I get a payload sometimes that has an empty record in the CSV so I just need to skip this itaration. When it gets to this line: queue_dir_path = os.path.join(output_dir_path, queue) This is were I have the issue. and get the error:

queue_dir_path = os.path.join(output_dir_path, queue) UnboundLocalError: local variable 'queue' referenced before assignment

How can I do this?

    # setting today as the date to name the ouput directory
    today = date.today()
    # joining the output_root_dir_path and today as the output directory
    todays_dir_path = os.path.join(output_root_dir_path, today.strftime("%m%d%Y"))

    logging.debug("Checking for directory: {}".format(todays_dir_path))
    # if the directory does not exist, create it
    if not os.path.isdir(todays_dir_path):
        logging.debug("{} not found - creating".format(todays_dir_path))
        os.makedirs(todays_dir_path)
    # creating a temp directory to unzip the zip file
    tempdir = tempfile.mkdtemp(dir=output_root_dir_path   '\\tmp', prefix="TEMP")

    logging.debug("Created temporary directory: {}".format(tempdir))
    # building the path to the unzipped file
    new_zip_file_path = os.path.join(tempdir, os.path.basename(zip_file_path))

    logging.debug("Moving ZIP file: {} -> {}".format(zip_file_path, new_zip_file_path))
    # moving the zip file to the temp directory
    shutil.move(zip_file_path, new_zip_file_path)

    logging.info("ZIP file moved to: {}".format(new_zip_file_path))

    tempdir2 = os.path.join(tempdir, str(time.time()))
    os.mkdir(tempdir2)
    # unzipping the file into the temp directory
    try:
        zip_f = zipfile.ZipFile(new_zip_file_path)
    except Exception as e:
        logging.error("Failed opening zip file with error: {}".format(e))
        return
    # setting csv to an empty list
    csv_file_path = None
    # looping through the files in the zip file
    for file in zip_f.namelist():
        zip_f.extract(file, path=tempdir2)

        extracted_path = os.path.join(tempdir2, file)
        # if the file is a csv file, set the csv_file_path to the extracted path
        if file.endswith(".csv") and os.path.isfile(extracted_path):
            csv_file_path = extracted_path
    # setting the output file path
    output_dir_path = os.path.join(
        output_root_dir_path, os.path.join(today.strftime("%m%d%Y"))
    )

    logging.debug("Output dir path: {}".format(output_dir_path))
    # if the output_dir_path is not a directory
    if not os.path.isdir(output_dir_path):
        #log that we are creating, and create the directory
        logging.debug("Creating output dir")
        os.makedirs(output_dir_path)
    # no idea why they use assert here Jason
    assert csv_file_path is not None
    # define a dictionary of record id's
    record_ids_by_filename = dict()
    queue_by_filename = dict()
    # open the csv file and read it
    csv_f = open(csv_file_path, "r", encoding="utf-8")
    csv_reader = csv.DictReader(csv_f)
    #loop through the rows in the csv file
    for row in csv_reader:
        record_id = row.get("RECORDDETAILID")
        pdf_filename = row.get("PRODUCT")
        queue = row.get("QUEUE")
        #
        if record_id is None or pdf_filename is None:
            continue
        record_ids_by_filename[pdf_filename] = record_id
        if queue is not None:
            queue_by_filename[pdf_filename] = queue
        logging.debug(row)
    queue_dir_path = os.path.join(output_dir_path, queue)
    if not os.path.isdir(queue_dir_path):
        os.makedirs(queue_dir_path)
    new_csv_file_path = os.path.join(queue_dir_path, os.path.basename(csv_file_path))
    logging.info("Copying CSV file: {} -> {}".format(csv_file_path, new_csv_file_path))
    shutil.copy(csv_file_path, new_csv_file_path)
    output_zip_file_path = os.path.join(queue_dir_path, os.path.basename(new_zip_file_path))
    logging.info(
        "Copying ZIP file: {} -> {}".format(new_zip_file_path, output_zip_file_path)
    )
    shutil.copy(new_zip_file_path, output_zip_file_path)
    for root, dirs, files in os.walk(tempdir2):
        for f in files:
            record_id = record_ids_by_filename.get(f)
            queue = queue_by_filename.get(f)
            if record_id is None or queue is None:
                continue
            f = os.path.join(root, f)
            # TODO This is a possible point to assemble the
            record_dir_path = os.path.join(
                output_dir_path, os.path.join(queue, record_id)
            )
            if not os.path.isdir(record_dir_path):
                logging.info("Creating directory: {}".format(record_dir_path))
                os.makedirs(record_dir_path)
            new_filepath = os.path.join(record_dir_path, os.path.basename(f))
            logging.info("Moving PDF file: {} -> {}".format(f, new_filepath))
            shutil.move(f, new_filepath)

    logging.info("Removing temporary directory {}...".format(tempdir))
    os.chdir(output_root_dir_path)
    #shutil.rmtree(tempdir)
    return tempdir

CodePudding user response:

The error says it all. The queue variable is undefined. queue's scope is inside the loop, and you are using it outside. Either define it or use it in the right scope.

CodePudding user response:

Your error is in the queue variable and is in the loop's scope which means that it can only be used in the loop. Use it in the correct scope by either making it global in some way(bad practice) or use it in the right scope.

  • Related