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
相关产品推荐
相关产品推荐

