[Tutor] Error in executing: cmd.

Alan Gauld alan.gauld at btinternet.com
Sat May 31 02:24:09 CEST 2008


"Teresa Von Hazmburg" <teresav at vtnnv.com> wrote

> Here are the files I was referring to in my last email

I don't really know the cause of your specific error but here
are some comments that will tidy the code somewhat and
might change the behaviour too.

#########################
#Open/read textfile
file = open (myfile, "r")

Best to avoid using file as a variable name sionce its a
built in name that is an alias for open. Using it as variable
will likely confuse things for readers.

file.seek(0)

No need for the seek() here, you are already at the beginning, remove 
it.

print file.readlines()
file.seek(0)
readlist = file.readlines()
#print readlist
file.seek(0)

Reverse the order to store the value first then print the value
That will  save 2 more seek() calls.

#listlength = len(readlist)
#bksec_list = []

for line in file.readlines():

But in fact you didn't even need those readlines at all.
Here you can just iterate over te file:

for line in file:


    print "book = " + line[0:3] + " and section = " + line[3:5]
    book = line[0:3]
    section = line[3:5]

Again if you do the assignments first then the print you
avoid repeating the slice action. and avoid a maintenance
problem since you can just print the actual variable values.


    bksecFolder = gismoFolder + "d" + book + "\\d" + book + section
    bkFolder = gismoFolder + "d" + book

Again do the assignmment first then use it in the bksecFolder 
assignment.
In other words reverse the order and use the variable.

You can probably speed it up even more by using string
formatting rather than addition which tends to be fairly slow
in Python.

    out_shp = bksecFolder + "\\parcel_p.shp"
    strSel = "\"PARCEL\" LIKE '"  + book + section + "%'"

I would consider printing the strSel value since that is what
you feed to Select_Analysis which appears to be the thing
that does the work. This is the most likely source of errors!

    #print "querying section " + book + "-" + section
    #print out_shp
    gp.Select_analysis(in_parcel_shp, out_shp, strSel)

print "Process finished"
# added to allow inspection of screen
print "PROCESS COMPLETE"

Only one print needed.

choice = raw_input('Type q to end    :')            #
if choice == 'q' :          #
        print ' '

The last if statement is redundant, lose it.

Those changes will trim the code, make it easier to read
and faster to execute. And will show us what the selection
string actually looks like.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list