1

我编写了几个简单的命令行/终端 Python 程序来解析我们在心理学实验室中使用的实验运行软件中的数据文件。这些程序将由通常精通计算机的人使用,但不一定能使用带有标志的完整 unix 样式命令,所以我通常只让程序问诸如“您要处理哪个文件? " 并让用户从列表中选择,如下所示:

import textwrap
import os
import sys

def get_folder():
    """ Print a numbered
    list of the subfolders in the working directory
    (i.e. the directory the
    script is run from),
    and returns the directory
    the user chooses.
    """
    print(textwrap.dedent(
    """
    Which folder are your files located in?
    If you cannot see it in this list, you need
    to copy the folder containing them to the
    same folder as this script.
    """
    )
    )
    dirs = [d for d in os.listdir() if os.path.isdir(d)] + ['EXIT']
    dir_dict = {ind: value for ind, value in enumerate(dirs)}
    for key in dir_dict:
        print('(' + str(key) + ') ' + dir_dict[key])
    print()
    resp = int(input())
    if dir_dict[resp] == 'EXIT':
        sys.exit()
    else:
        return dir_dict[resp] 

Python 库中是否有这些类型的文件选择器的实现?我通常只是在需要时自己快速实现它们,但我最终不得不将代码从一个文件复制并粘贴到另一个文件,并针对我使用它们的特定情况进行修改。

4

0 回答 0