Python脚本通过脚本中的每个函数编写和附加一个xls文档。目前它正在重写sh

2024-06-23 02:46:17 发布

您现在位置:Python中文网/ 问答频道 /正文

Python脚本通过脚本中的每个函数编写和附加一个xls文档。目前它正在覆盖工作表。在

代码如下:

import os
import os.path
import MySQLdb
import configuration
import hashlib
import xlsxwriter
from lxml import etree


def Check():
    try:
        PATH='config.txt'
        workbook = xlsxwriter.Workbook('demo.xlsx')
                worksheet = workbook.add_worksheet()
                worksheet.set_column('A:A', 20)
                bold = workbook.add_format({'bold': True})
                worksheet.write('A1', 'File Name',bold)
                worksheet.write('B1', 'is Present', bold)
                worksheet.write(2, 1, 'yes')
                worksheet.write('D1', 'Verification', bold)
                worksheet.write(2, 0, 'config.txt')


        if os.path.isfile(PATH) and os.access(PATH, os.R_OK):

                print "config.txt file exists and is readable"
            global cur1
                    var = configuration.client['client_id']
                    host = configuration.mysql_host['host']
                    username = configuration.mysql_username['username']
                    password = configuration.mysql_password['password']
                    connection=MySQLdb.connect( user=username, passwd=password, db="db")
                    cur1=connection.cursor()
                    cur1.execute("select md5sum from **** where file_name = 'config.txt' and sd = '%s'" % var)
                    row=cur1.fetchone()
                    ele = 'ncconfig.txt'
                    for md5 in row:
                            file_name = 'config.txt'
                            with open(file_name) as file_to_check:
                                    data = file_to_check.read()
                                    md5_returned = hashlib.md5(data).hexdigest()
                            if md5 == md5_returned:
                                    print('File %s md5 has been verified' % ele)
                                worksheet.write(2, 3, 'verified')
                                workbook.close()

                            else:
                                    print('File %s md5 is not matching....So syncing failed ' % ele)
                                worksheet.write(2, 3, 'Failed')
                                workbook.close()

            cur1.close()
                    connection.close()

        else:
                print "Either config.txt file is missing or is not readable"
                        worksheet.write(2, 3, 'Failed')
                        worksheet.write('G1','Reason', bold)
            worksheet.write(2,5,'May be file is not present')
                        workbook.close()    
    except MySQLdb.Error, e:
                print "Error %d: %s" % (e.args[0], e.args[1])





def Check_md5_for_rconfig():
        try:
                PATH='rconfig.txt'
                if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
                        print "rconfig.txt file exists and is readable"
                        global cur1
                        var = configuration.client['client_id']
                        host = configuration.mysql_host['host']
                        username = configuration.mysql_username['username']
                        password = configuration.mysql_password['password']
                        connection=MySQLdb.connect( user=username, passwd=password, db="db")
                        cur1=connection.cursor()
                        cur1.execute("select md5sum from **** where file_name = 'rconfig.txt' and sd = '%s'" % var)
                        row=cur1.fetchone()
                        ele = 'rconfig.txt'
                        for md5 in row:
                                file_name = 'rconfig.txt'
                                with open(file_name) as file_to_check:
                                        data = file_to_check.read()
                                        md5_returned = hashlib.md5(data).hexdigest()
                                if md5 == md5_returned:
                                        print('File %s md5 has been verified' % ele)



                                else:
                                        print('File %s md5 is not matching....So syncing failed ' % ele)


                        cur1.close()
                        connection.close()

                else:
                        print "Either rconfig.txt file is missing or is not readable"

        except MySQLdb.Error, e:
                print "Error %d: %s" % (e.args[0], e.args[1])

Tags: importtxtcloseisosusernamepasswordconfiguration
1条回答
网友
1楼 · 发布于 2024-06-23 02:46:17

更新这将回答您的问题: Overflow Answer Here! 我刚读到xlmswriter不会修改现有的excel文件,但链接中有一个用于修改的模块

with open(file_name) as file_to_check:

应该是

^{pr2}$

对于两个读取实例

您是否试图附加到工作表中?从我看到的每次check()被执行时,它都会写入与之前相同的文件名工作表.write将数据插入到以前定义的位置,这会覆盖文件中的现有数据,我还没有使用xlswriter,所以可能会追加,但我看到的就是这个。在

相关问题 更多 >