# coding=utf-8 import pymysql import os host="localhost" user="root" password="root" db="exam" exceptTables=["exam_question_type",'exam_function','exam_admin_group'] conn = pymysql.connect(host=host,user=user,password=password,database=db,charset="utf8") with conn.cursor() as cursor: cursor.execute('show tables') rs = cursor.fetchall() for row in rs: cmd = "TRUNCATE " cmd += row[0] #print cmd if(row[0] in exceptTables): continue cursor.execute(cmd)