脚本应该接受用户输入并以特定格式打印文件夹的所有内容。示例:Folder1(主文件夹) - > Folder2 - > Folder3 - > Folder4。
目录包含格式为助记符:内容
的助记符(Folder1)内容可能包含子文件夹。
我在这里抛出这个错误: Valueerror要解压的值太多(预期2,得1)
在这一行:" 表示single_mnemonic,os.listdir(startpath)中的内容:"
def search_specific_mnemonic(startpath):
user_entered_mnemonic = input("Enter Client Mnemonic: ")
facility_types = ["clinic", "hospital", "lab", "hub_millennium", "client_millennium"]
for single_mnemonic, contents in os.listdir(startpath):
CRED = "\033[91m"
CEND = "\033[0m"
print(CRED + "\n",user_entered_mnemonic + CEND)
for facility_type in facility_types:
if contents[facility_type]:
box_display_char = "X"
else:
box_display_char = "_"
print(" [{0}] - {1}".format(box_display_char, facility_type.replace("_", " ").title()))
if contents[facility_type]:
for practice_name in sorted(contents[facility_type]):
print(" {}".format(practice_name))
for practice_scripts in sorted(contents[facility_type]):
print(" {}".format(contents[facility_type][practice_name]))
break
答案 0 :(得分:0)
如果您确定文件夹中的所有文件名具有相同的格式
(即助记符:内容)
你可以试试:
for single_mnemonic, contents in [format_name.split(':') for format_name in os.listdir(startpath):
这将返回一个数组,其中每个项目都是格式化名称两边的数组:[[mnemonic,content],[mnemonic,content]]