You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3.6创建员工ID训练图像文件夹时出现语法错误求助

解决Python创建员工ID训练图像文件夹时的无效语法错误

嘿,我一眼就揪出你代码里的问题啦!你遇到的无效语法错误,根源是定义foldername的那一行少了一个右括号。

看你原来的代码这一行:

foldername = os.path.join(config.Subject_Dir, Filename + '%03d' % (config.Subject_Id)

这里的% (config.Subject_Id)后面没有闭合的),Python解析代码时没法识别完整的语句结构,直接就抛出了语法错误。

修正后的完整代码如下:

if __name__ == '__main__':
    image = []
    camera = config.get_camera()
    # Create the directory for positive training images if it doesn't exist.
    if not os.path.exists(config.Subject_Dir):
        os.makedirs(config.Subject_Dir)
    # 补上缺失的右括号,修复语法错误
    foldername = os.path.join(config.Subject_Dir, Filename + '%03d' % (config.Subject_Id))
    # Create the folder for specific Employee ID training images if it doesn't exist.
    if not os.path.exists(foldername):
        os.makedirs(foldername)
    # Find the largest ID of existing images.
    # Start new images after this ID value.
    files = sorted(glob.glob(os.path.join(foldername,'[0-9][0-9][0-9]' + '.pgm')))
    count = 0

顺便给你个小提示:写嵌套括号的代码时,写完可以快速扫一眼左右括号是否配对,现在大部分IDE(比如VS Code、PyCharm)都会用颜色高亮提示括号配对,能帮你快速避开这类低级错误~

内容的提问来源于stack exchange,提问作者Ramya Bhagirathi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:33:43