异常
import traceback
try:
pass
except Exception:
traceback.print_exc()
操作数据库
# pip install PyMySQL
import pymysql
db = pymysql.connect(host='127.0.0.1', user='root', password='', database='test', charset='utf8mb4')
cursor = db.cursor()
for word in words_list:
try:
cursor.execute('select id from forbidden_words where words = %s limit 1', (word,))
result = cursor.fetchone()
try:
if result is None:
cursor.execute('insert into forbidden_words(words) values (%s)', (word,))
db.commit()
except:
print("写入失败")
db.rollback()
except:
print("查询失败")
db.close()
创建临时文件
tmp_file = tempfile.TemporaryFile("r+")
tmp_file.write(open("file_name", encoding="utf-8").read())
tmp_file.seek(0)
获取文件名和后缀
# input/a.csv
fileArr = os.path.splitext(os.path.basename(inputFile))
# fileArr[0] a
# fileArr[1] .csv