1. 程式人生 > >今天發現python windows執行正常,linux下異常

今天發現python windows執行正常,linux下異常

#--coding:gb18030--
#filename:man_pon_alarms.py
import pymysql.cursors
import xlrd
import os
import time
import glob
# Connect to the mysql database
#import os,os.path,datetime
def main():
    base_dir=r"/home/pon/"
    l=os.listdir(base_dir)   
    #confirm the dirpath has xls file
    if (''.join(l).find(".xls"))>0:     
        l.sort(key=lambda fn: os.path.getmtime(base_dir+fn) if not os.path.isdir(base_dir+fn) else 0)
        workbook = xlrd.open_workbook(base_dir+l[-1])
        worksheet1 = workbook.sheets()[0]
        num_cols = worksheet1.ncols
        num_rows = worksheet1.nrows
        num_cols = worksheet1.ncols
        connection = pymysql.connect(host='175.155.1.3',
                                         user='popon',
                                         password='pon-pop-3',
                                         db='db_pon_alarm',
                                         charset='utf8mb4',
                                         cursorclass=pymysql.cursors.DictCursor)
        try:
                with connection.cursor() as cursor:
                    for rown in range(3,num_rows-1):
                        # Create a new record
                        sql = "INSERT INTO tb_PonAlarms(NetCell, NetCellLocal,AlarmCode,AlarmNote,AlarmTime,AlarmText,NetIP) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')"
                        cursor.execute(sql%(worksheet1.cell_value(rown,2), worksheet1.cell_value(rown,3),worksheet1.cell_value(rown,6),worksheet1.cell_value(rown,7),worksheet1.cell_value(rown,8),worksheet1.cell_value(rown,11),worksheet1.cell_value(rown,23)))
                        connection.commit()
                        print("read alarms succed")
        finally:
                    connection.close()
                    for filename in os.listdir(base_dir):
                        if '.xls' in filename:
                             os.remove(base_dir+filename)


def timer(n):
    while True:
        print('program round start at:',time.strftime('%Y-%m-%d %X',time.localtime()))
        main()
        print('program round finished at:',time.strftime('%Y-%m-%d %X',time.localtime()))
        print('next round will start after %d second'%n)
        time.sleep(n)

if __name__ == '__main__':
    timer(600)