简体   繁体   English

Python-用户输入目录中的特定文件夹,然后打印目录的所有内容

[英]Python - User input for specific folder in directory, then print all contents of directory

Scripts is supposed to accept user input and print out all contents of a folder in a specific format. 脚本应该接受用户输入并以特定格式打印出文件夹的所有内容。 Example: Folder1(main folder)-->Folder2-->Folder3-->Folder4. 示例:Folder1(主文件夹)-> Folder2-> Folder3-> Folder4。

Directory contains mnemonics(Folder1) with format mnemonic:contents 目录包含助记符(Folder1),其格式为mnemonic:contents

Contents may contain subfolders. 内容可能包含子文件夹。

What I have here is throwing this error: Valueerror too many values to unpack (expected 2, got 1) 我在这里抛出的错误是: Valueerror太多的值无法解包(预期2,得到1)

in this line: " for single_mnemonic, contents in os.listdir(startpath): " 在这一行:“ 对于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

If you are certain all file names in your folder are of the same format 如果确定文件夹中的所有文件名都具有相同的格式
(ie mnemonic:content) (即助记符:内容)
You could try: 您可以尝试:
for single_mnemonic, contents in [format_name.split(':') for format_name in os.listdir(startpath):

This will return an array, where's every item is a an array of the two sides of the formatted name: [[mnemonic, content], [mnemonic, content]] 这将返回一个数组,每个项目都是格式化名称两侧的数组:[[助记符,内容],[助记符,内容]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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