如何仅使用.xlsx扩展名验证文件名



我有一个基于粘糊糊的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!'
                                 }
                             })

然而,它给我一个"测试"行的语法错误无效:'str(user_input(== .xlsx'

使用 str.endswith

ex:

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!'
                                 }
                             })

最新更新