我写了两个简单的命令行/终端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库中是否有这种文件选择器的实现?我通常只是在需要的时候自己快速地实现它们,但最后不得不将代码从一个文件复制粘贴到另一个文件,然后根据我使用它们的特定情况进行修改。在