FAQ
I am getting terribly confused. Here is the code I am using for an SQL DB
query (this may look familiar to those who have helped me in the past):

import time, os, sys
from strptime import *
import dbi, odbc


if os.path.exists(os.environ['SystemDrive'] + '/data'):
pass
else:
os.mkdir(os.environ['SystemDrive'] + '/data')
servername = ['ServerA', 'ServerB', 'ServerC']
print "This program pulls data from all servers for name and dates of
choice."
name = ['None', 'nameA', 'nameB', 'nameC', 'nameD', 'nameE', 'nameF',
'nameG', 'nameH', 'nameJ', 'nameK', 'nameL', 'nameM']
choose = input("Select family name:\n\
1: nameA\n\
2: nameB\n\
3: nameC\n\
4: nameD\n\
5: nameE\n\
6: nameF\n\
7: nameG\n\
8: nameH\n\
9: nameJ\n\
10: nameK\n\
11: nameL\n\
12: nameM\n")
start = raw_input("Start date? (YYYY-MM-DD):")
stop = raw_input("Stop date? (YYYY-MM-DD):")
check1 = time.mktime(strptime(start, '%Y-%m-%d'))
check2 = time.mktime(strptime(stop, '%Y-%m-%d'))
if check1 > check2:
print "Start date occurs after stop date!"
time.sleep(5)
sys.exit()
else:
pass
print "Working"
for index in servername:
try:
alt = 1
verify = 0
if os.path.exists(os.environ['SystemDrive'] + '/data/' +
name[choose] + '_' + index + '.txt'):
verify = 1
answer = raw_input("File " +
os.environ['SystemDrive'] + '/data/' + name[choose] + '_' + index + '.txt' +
" already exists. Overwrite? (y/n):")
if answer in ('y', 'Y'): verify = 0
if answer in ('n', 'N'):
alt = 0
alternate = raw_input("Enter desired file
name (no extension):")
dbc = odbc.odbc(index + '/username/password')
crsr = dbc.cursor()
crsr.execute("""select * from subtestlist\n\
where (errorcode<>'null'\n\
AND familyname='"""+name[choose]+"""'\n\
AND PackageStartTime>'"""+start+""" 00:00:00.00'\n\
AND PackageStartTime<='"""+stop+""" 23:59:59.99');""")
result = crsr.fetchall()
while verify == 1:
if os.path.exists(os.environ['SystemDrive'] +
'/data/' + alternate + '_' + index + '.txt'):
answer = raw_input("File " +
os.environ['SystemDrive'] + '/data/' + alternate + '_' + index + '.txt' + "
already exists. Overwrite? (y/n):")
if answer in ('y', 'Y'): verify = 0
if answer in ('n', 'N'):
alternate = raw_input("Enter desired
file name (no extension):")
else:
verify = 0
if alt == 1:
if os.path.exists(os.environ['SystemDrive'] +
'/data/' + name[choose] + '_' + index + '.txt'):
os.remove(os.environ['SystemDrive'] +
'/data/' + name[choose] + '_' + index + '.txt')
f = open(os.environ['SystemDrive'] + '/data/' +
name[choose] + '_' + index + '.txt', 'a')
else:
f = open(os.environ['SystemDrive'] + '/data/' +
alternate + '_' + index + '.txt', 'a')
f.write("modulename, subtestnum, subtestname, familyname,
releasenum, productcode, serialnum, servername, id, subtestendtime,
packagestarttime, opid, errorcode, errormessage, isfinalsubtest, duration,
firstsn" + '\n')
z = 0
for item in range(len(result)):
modulename, subtestnum, subtestname, familyname,
releasenum, productcode, serialnum, servername, id, subtestendtime,
packagestarttime, opid, errorcode, errormessage, isfinalsubtest, duration,
firstsn = result[item]
subtestendtime = time.ctime(subtestendtime)
packagestarttime = time.ctime(packagestarttime)
f.write(`modulename` + ',' + `subtestnum` + ',' +
`subtestname` + ',' + `familyname` + ',' + `releasenum` + ',' +
`productcode` + ',' + `serialnum` + ',' + `servername` + ',' + `id` + ',' +
`subtestendtime` + ',' + `packagestarttime` + ',' + `opid` + ',' +
`errorcode` + ',' + `errormessage` + ',' + `isfinalsubtest` + ',' +
`duration` + ',' + `firstsn` + '\n')
z = z + 1
f.write('\n' + "Total error count = " + `z`)
f.close()
except dbi.opError:
servername.append(index)
print servername
print "Files written for import to Excel. Location: " +
os.environ['SystemDrive'] + "\data"
time.sleep(7)


(Names have been changed to protect the innocent...namely, me)

Here is the output from IDLE:

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
import query
This program pulls data from all servers for name and dates of choice.
Select family name:
1: nameA
2: nameB
3: nameC
4: nameD
5: nameE
6: nameF
7: nameG
8: nameH
9: nameJ
10: nameK
11: nameL
12: nameM
1
Start date? (YYYY-MM-DD):2000-04-01
Stop date? (YYYY-MM-DD):2000-05-16
Working
['ServerA', 'ServerB', 'ServerC', 'ServerA']
Traceback (innermost last):
File "<pyshell#0>", line 1, in ?
import query
File "C:\PROGRA~1\Python\query.py", line 87, in ?
servername.append(index)
AttributeError: 'string' object has no attribute 'append'
>>>


What I don't understand is, why did the append work the first time (as
evidenced by the list printout after 'Working') and not work the second
time?

Please forgive the crude interface code, it was the only way I knew how to
do it at the time, but, "I'm getting better!"

TIA,

- Mark

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 2 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedMay 16, '00 at 6:41p
activeMay 16, '00 at 6:46p
posts2
users2
websitepython.org

2 users in discussion

Daley, MarkX: 1 post Fredrik Lundh: 1 post

People

Translate

site design / logo © 2022 Grokbase