alex podolin
alex podolin

Reputation: 9

why does my python script write only last line from SELECT sql query

I have a python code, which do a SELECT from mysql database, then write result to files. In result, files /tmp/ifcfg-* is ok, but in file /tmp/route i see only last line from database query, it look like:

10.130.48.0/23 via 10.130.48.7

but i expect

10.130.48.0/23 via 10.130.48.7
192.168.156.0/22 via 192.168.156.91
10.16.234.0/29 via 10.16.234.2
10.16.234.8/29 via 10.16.234.10

what is the reason ?

    get_settings = "SELECT id_srv, \
                       vlan, \
                       phys_dev_srv, \
                       onboot, \
                       inet_ntoa(subnet_ipv4), \
                       prefix, \
                       inet_ntoa(int_ipv4), \
                       inet_ntoa(dns_srv_01), \
                       inet_ntoa(dns_srv_02), \
                       dns_domain \
                       FROM interfaces_ipv4 WHERE id_srv = '%i'" % (curr_srv_id);
cursor = conn.cursor()
cursor.execute(get_settings)
rows = cursor.fetchall()
for row in rows:
    with open('/tmp/ifcfg-' + row[2] + '.' + str(row[1]), 'w+') as result, \
         open('/tmp/route', 'w+') as route:
        result.write('# vlan ' + str(row[1]) + '\n')
        result.write('VLAN=yes' + '\n')
        result.write('BOOTPROTO=static' + '\n')
        result.write('NAME=' + row[2] + '.' + str(row[1]) + '\n')
        result.write('DEVICE=' + row[2] + '.' + str(row[1]) + '\n')
        result.write('PHYSDEV=' + row[2] + '\n')
        result.write('ONBOOT=' + row[3] + '\n')
        result.write('IPADDR=' + row[6] + '\n')
        result.write('PREFIX=' + str(row[5]) + '\n')
        result.write('DNS1=' + row[7] + '\n')
        result.write('DNS2=' + row[8] + '\n')
        result.write('DOMAIN=' + row[9] + '\n')

        route.write(row[4] + '/' + str(row[5]) + ' via ' + row[6] +'\n')

Upvotes: 0

Views: 42

Answers (1)

Rakesh
Rakesh

Reputation: 82785

Place the with statement outside the for-loop

Ex:

cursor = conn.cursor()
cursor.execute(get_settings)
rows = cursor.fetchall()
with open('/tmp/ifcfg-' + row[2] + '.' + str(row[1]), 'w+') as result, open('/tmp/route', 'w+') as route:
    for row in rows:
        result.write('# vlan ' + str(row[1]) + '\n')
        result.write('VLAN=yes' + '\n')
        result.write('BOOTPROTO=static' + '\n')
        result.write('NAME=' + row[2] + '.' + str(row[1]) + '\n')
        result.write('DEVICE=' + row[2] + '.' + str(row[1]) + '\n')
        result.write('PHYSDEV=' + row[2] + '\n')
        result.write('ONBOOT=' + row[3] + '\n')
        result.write('IPADDR=' + row[6] + '\n')
        result.write('PREFIX=' + str(row[5]) + '\n')
        result.write('DNS1=' + row[7] + '\n')
        result.write('DNS2=' + row[8] + '\n')
        result.write('DOMAIN=' + row[9] + '\n')

        route.write(row[4] + '/' + str(row[5]) + ' via ' + row[6] +'\n')

Your approach is overriding the content of the file because you are opening the file each time in the forloop and writing content again.

Upvotes: 1

Related Questions