특정 폴더를 파싱하고 찾는 기능이 있습니다.부울 체크 사전
목표 : 사전에 이러한 폴더가있는 경우 녹색 (폴더 있음) 또는 빨간색 (폴더 없음)으로 인쇄하고 싶습니다. 사전에 부울 검사를하는 데 문제가 있습니다. 나는 파이썬에 상당히 익숙하다.
def parse_directory(startpath):
for dir_path, folders, filenames in os.walk(startpath):
path_level = dir_path.replace(startpath, '').count(os.sep)
relative_dir = dir_path.replace(startpath, '').strip(os.sep)
current_dir = dir_path.split(os.sep)[-1]
if path_level == 1:
mnemonic = relative_dir
client_dictionary[mnemonic] = {}
if "4x Clinic" in folders:
client_dictionary[mnemonic]["clinic"] = True
else:
client_dictionary[mnemonic]["clinic"] = False
if "4x Hospital" in folders:
client_dictionary[mnemonic]["hospital"] = True
else:
client_dictionary[mnemonic]["hospital"] = False
if "4x Lab" in folders:
client_dictionary[mnemonic]["lab"] = True
else:
client_dictionary[mnemonic]["lab"] = False
if "Hub Millennium" in folders:
client_dictionary[mnemonic]["hub_millennium"] = True
else:
client_dictionary[mnemonic]["hub_millennium"] = False
if "Client Millennium" in folders:
client_dictionary[mnemonic]["client_millennium"] = True
else:
client_dictionary[mnemonic]["client_millennium"] = False
위의 코드는 구문 분석합니다. 이제 불리언 체크를하기 위해서 1에서 get_status
까지 그리고 다른 하나는 print_colored_mnemonics
에서 mneonics (녹색이 있다면 녹색)를 빨간색 (존재하지 않음)으로 인쇄합니다.
def print_colored_mnemonics(startpath):
RED = "\033[91m"
RED2 = "\033[0m"
GREEN = "\033[92m"
GREEN2 = "\033[0m"
if get_status(startpath) == True:
print(GREEN + '{}'.format(dict_keys) + GREEN2)
if get_status(startpath) == False:
print(RED + '{}'.format(dict_keys) + RED2)
나는이 권리를하고 있는가 :
여기def get_status(startpath):
for dict_keys, dict_values in parsing_dict.items():
if all(dict_values):
return True
elif any(dict_values):
return False
가 print_colored_mnemonics
코드 : 여기
get_status
코드? 나는 본질적으로 어떤 내용이 존재한다면 사전 (니모닉)의 키가 녹색이되기를 원한다. 그리고 키에 내용이 없다면 니모닉이 빨간색으로 인쇄되어야합니다.
실제로 어떤 문제가 있습니까? –
@AustinHastings 코드가 제대로 작동하지 않습니다.실제로 코드를 보관하지 않는 폴더가있을 때 코드는 녹색으로 모든 니모닉을 인쇄합니다. – FFF
첫 번째 코드 블록은'get_filtered_mnemonics' 함수로되어 있습니까? –