Python has a logging module to facilitate the recording of program action logs, this article is a simple example of the use of the logging module:
First introduce logging:
Import xlrd
Import MySQLdb
Import OS
Import logging
First initialize the configuration in the main program, such as:
if __name__ = = "__main__":
Logging.basicconfig (filename= './log.txt ', level=logging. DEBUG)
Path = './excel '
Main (PATH)
Specifies the file that holds the log, which is the Log.txt file log in the current directory. Where you need to record the operation:
Try
C.executemany (
' "'" INSERT into ' order ' VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%. %s,%s,%s) "" ",
Data
Db.commit ()
Except Exception as E:
Logging.info (e)
Db.close ()
Or
def main (path):
"""
Open Directory traverse Excel file and store to MySQL
"""
Files = Os.listdir (path)
For file in Files:
Save_file (path + '/' + file)
Print (file)
Logging.info (file)
Look at the highlighted section of the code so that the log is logged to the specified file.