我有一个基于 Gooey 的 GUI,用户可以在其中输入要在程序末尾创建的输出文件的名称。但是,我希望只能添加最后带有 .xlsx 扩展名的文件名。
这是我的验证器:
parser.add_argument('-Choose_File_Name',
action='store',
help="Output File Name with .xlsx",
gooey_options={
'validator': {
'test': 'str(user_input) == .xlsx',
'message': 'Must contain .xlsx at the end!'
}
})
但是,它给了我“test”行无效的语法错误:“str(user_input) == .xlsx”
最佳答案
使用str.endswith
例如:
parser.add_argument('-Choose_File_Name',
action='store',
help="Output File Name with .xlsx",
gooey_options={
'validator': {
'test': 'user_input.endswith(".xlsx") == True',
'message': 'Must contain .xlsx at the end!'
}
})
关于python - 如何仅验证扩展名为 .xlsx 的文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57305083/