我每天都将多个csv文件加载到数据库表中。 源文件位于“源”文件夹中 加载完成后,移至Archieve文件夹。 代码运行正常。 我很难在这段代码中编写异常错误处理。 请帮忙。 预先感谢。
我的代码的简单格式:
Try:
Code to load
Except:
Logger.exception
Else:
Code to move file
我的原始代码:
import logging
import logger
import glob
import os
import pandas as pd
import time
import numpy as np
import shutil
import os
source = "C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37-32\\Scripts\\SourceFiles\\"
file_titles = ['C1','C2']
all_files = glob.glob(source + "/*Sales*.csv")
li = []
for filename in all_files:
df = pd.read_csv(filename, index_col=None, header=None,delimiter=',',names=file_titles,na_filter=False,skiprows=1,low_memory=False)
li.append(df)
frame = pd.concat(li, axis=0, ignore_index=True)
Reporting_Date = time.strftime("%Y-%m-%d")
todaysdate = time.strftime("%Y%m%d")
df['CreatedDate'], df['CreatedBy'], df['LastModifiedDate'], df['LastModifiedBy'] = [todaysdate, 'User',todaysdate,'User']
try:
import pyodbc
sql_conn = pyodbc.connect(
"Driver={SQL Server};"
"Server=Server1;"
"Database=DB;"
"Trusted_Connection=yes;")
cursor = sql_conn.cursor()
for index,row in df1.iterrows():
cursor.execute("INSERT INTO Table_Name ('C1','C2') Values (?,?)",
row['C1'],
row['C2']
)
sql_conn.commit()
cursor.close()
sql_conn.close()
print('Data Loading into table Table_Name is successful...')
except: # something else went wrong
#set different formats for logging output
console_logging_format = '%(levelname)s:  %(message)s'
file_logging_format = '%(levelname)s: %(asctime)s: %(message)s'
# configure logger
logging.basicConfig(level=logging.DEBUG, format=console_logging_format)
logger = logging.getLogger()
# create a file handler for output file
handler = logging.FileHandler('console_and_file.log')
# set the logging level for log file
handler.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter(file_logging_format)
handler.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
# output logging messages
logger.info("save this to the log - info logger")
logger.debug("save this to the log - debug logger")
logger.error("save this to the log - error - logger")
logger.warning("save this to the log - warning - logger")
logger.critical("save this to the log - critical - logger")
else:
dest1 = "C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37-32\\Scripts\\Archieve\\"
files = os.listdir(source)
for f in files:
shutil.move(source+f, dest1)