![](/img/trans.png)
[英]Code doesn't behave like conditions are being met when they are? [Python 3]
[英]Third ELIF statement doesn't execute when conditions are met. Executes after I re run code. Python
我有一個我無法解釋的錯誤。 我正在讀取配置文件以查看目錄是否存在。 如果不是,則腳本應該創建目錄,然后將該目錄寫入配置文件。 所以我寫了代碼:
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)
我的問題是配置文件為空時第三個 ELIF 不執行。 在我重新運行代碼之前,它將在 ELIF 語句中創建前兩個目錄,但不會創建最后一個目錄。 為什么是這樣?
這是因為Elif
僅在所有其他if
語句都失敗時才會發生,所以 IF 這沒有發生然后ELSE IF
。
您需要將ELIF
替換為IF
,然后無論其他 IF 語句如何,都會檢查條件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.