gpt4 book ai didi

python - 如何仅验证扩展名为 .xlsx 的文件名?

转载 作者:太空宇宙 更新时间:2023-11-03 20:33:51 26 4
gpt4 key购买 nike

我有一个基于 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com