简体   繁体   中英

Third ELIF statement doesn't execute when conditions are met. Executes after I re run code. Python

I have a bug I cannot explain. I am reading from a config file to see if a directory exists. If it doesn't the script is supposed to make the directory then write that directory to the config file. So I wrote the code:

def process_dirconfig_file(config_file_from_sysarg):
config = ConfigParser()
#print(type(config_file_from_sysarg))
config.read(config_file_from_sysarg)
dirconfig_file_Pobj = Path(config_file_from_sysarg)
try:
    if Path.is_file(dirconfig_file_Pobj):
        parseddict = {}
        configsects_set = set()
        for sect in config.sections():
            configsects_set.add(sect)
            for k, v in config.items(sect):
                # print('{} = {}'.format(k, v))
                parseddict[k] = v
        #print(parseddict)
        try:
            if ("base_dir" not in parseddict) or (parseddict["base_dir"] == ""):
                raise Exception(f"Error: Your config file is missing 'base directory' for file processing. Please edit config file to include base directory where csv are stored")
            elif("archive_dir" not in parseddict) or (parseddict["archive_dir"] == ""):
                base_dir = parseddict["base_dir"] #testconfig base = C:\Users\sys_nsgprobeingestio\Documents\dozie\odfs\odfshistory
                Path(base_dir+r"\archive").mkdir(parents=True, exist_ok=True)
                newarchive_dir = base_dir+r"\archive"
                #print(newarchive_dir)
                if "archive" not in configsects_set:
                    config_update = ConfigParser()
                    config_update.add_section("archive")
                    if "archive_dir" not in parseddict:
                        config_update.set("archive","archive_dir",newarchive_dir)
                    with open(config_file_from_sysarg, 'a') as f:
                        f.write('\n' * 2)
                        f.write("#archived csv folder path")
                        f.write('\n')
                        config_update.write(f)
                        parseddict["archive_dir"] = newarchive_dir
                #print(f"Error: Your config file is missing 'archive directory' for file processing")
            elif ("empty_dir" not in parseddict) or (parseddict["empty_dir"] == ""):
                base_dir = parseddict["base_dir"]  # testconfig base = C:\Users\sys_nsgprobeingestio\Documents\dozie\odfs\odfshistory
                Path(base_dir + r"\empty").mkdir(parents=True, exist_ok=True)
                newempty_dir = base_dir + r"\empty"
                if "empty" not in configsects_set:
                    config_update = ConfigParser()
                    config_update.add_section("empty")
                    if "empty_dir" not in parseddict:
                        config_update.set("empty", "empty_dir", newempty_dir)
                    with open(config_file_from_sysarg, 'a') as f:
                        f.write('\n' * 2)
                        f.write("#empty csv folder path")
                        f.write('\n')
                        config_update.write(f)
                        parseddict["empty_dir"] = newempty_dir
            elif ("error_dir" not in parseddict) or (parseddict["error_dir"] == ""):
                base_dir = parseddict["base_dir"]  # testconfig base = C:\Users\sys_nsgprobeingestio\Documents\dozie\odfs\odfshistory
                Path(base_dir + r"\error").mkdir(parents=True, exist_ok=True)
                newerror_dir = base_dir + r"\error"
                if "error" not in configsects_set:
                    config_update = ConfigParser()
                    config_update.add_section("error")
                    if "error_dir" not in parseddict:
                        config_update.set("error", "error_dir", newerror_dir)
                    with open(config_file_from_sysarg, 'a') as f:
                        f.write('\n' * 2)
                        f.write("#error csv folder path")
                        f.write('\n')
                        config_update.write(f)
                        parseddict["error_dir"] = newerror_dir
                #print(f"Error: Your config file is missing 'error directory' for file processing")

        except Exception as e:
            raise Exception(e)
        return parseddict
    else:
        raise Exception(f"Error: No directory config file. Please create a config file of directories to be used in processing")
except Exception as e:
    raise Exception(e)

My issue is that the third ELIF doesn't execute when the config file is empty. It will create the first two directories but not the last directory in the ELIF statement until I re-run the code. Why is this?

That's because Elif only happens when all other if statements have failed, so IF this doesn't happen then ELSE IF .

What you need is to replace the ELIF with IF , then the condition will be checked regardless of the other IF statements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM