【发布时间】:2016-03-08 10:06:36
【问题描述】:
我有一个基于 MySQLdb 游标的 while 循环,我需要根据 if 语句中的某些条件移动到下一次迭代。净化后的代码如下所示:
row = cur.fetchone()
while row is not None:
unique_id = row[0]
mac = row[1]
url = row[2]
location = old_path + unique_id
os.chdir(location)
file_count = 0
for file in os.listdir(location):
if file.startswith('phone.') and file.endswith('.cfg'):
file_count = file_count + 1
if file_count != 1:
userprompt = 'Multiple phone.MAC.RANDOM.cfg files detected continue?'
cont = query_yes_no(userprompt) # prompt user on what to do next - abort or continue
if cont == False:
debugmsg = 'User requested abort - aborting'
print debugmsg
logger.info(debugmsg)
sys.exit()
elif cont == True:
debugmsg = 'Moving to next on user request'
logger.info(debugmsg)
continue
此代码的行为与预期不符。运行时,如果它遇到与file_count !=1 条件匹配的目录,则循环似乎会再次运行,而不会前进到下一行。除非我误解了 continue 的用法,否则我认为它应该有效地退出循环的迭代并移至下一行。
我错过了什么?
【问题讨论】:
-
您确定
cont与True 或False 不同吗?例如。没有任何? (即您的 logger.info() 调用是否真的记录了消息?) -
也许我很敏感,但我不明白为什么这被否决了? :-/
-
@FrankSchmitt 是的,我确信
cont的值是由函数query_yes_no设置的
标签: python mysql mysql-python