Skip to content

Commit 5cec24b

Browse files
author
tuntun
committed
保存excel数据到mysql,增加try except防止异常中断,并记录log日志,把异常的文件复制一份到其他文件夹
1 parent afe5798 commit 5cec24b

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

save_excel_to_mysql.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import MySQLdb
44
import os
55
import logging
6+
import shutil
67
import sys
78

89

@@ -12,17 +13,21 @@ def main(path):
1213
"""
1314
files = os.listdir(path)
1415
for file in files:
15-
save_file(path + '/' + file)
16+
save_file(path + '/' + file, file)
1617
print(file)
17-
logging.info(file)
1818

1919

20-
def save_file(file):
20+
def save_file(file, filename):
2121
"""
2222
打开excel文件
2323
"""
24-
book = xlrd.open_workbook(file)
25-
sheet = book.sheet_by_index(0)
24+
try:
25+
book = xlrd.open_workbook(file)
26+
sheet = book.sheet_by_index(0)
27+
except Exception as e:
28+
logging.info('错误:{0} 文件:{1}'.format(e, file))
29+
shutil.copy2(file, './error' + filename)
30+
return False
2631
row_nums = sheet.nrows
2732
col_nums = sheet.ncols
2833

0 commit comments

Comments
 (0)