Different results when running script from IDLE versus Command Line

Casey T cpthomas at gmail.com
Thu Mar 13 09:05:45 EDT 2008


On Mar 12, 5:28 pm, Chris <cwi... at gmail.com> wrote:
> On Mar 12, 8:10 pm, Casey T <cptho... at gmail.com> wrote:
>
>
>
> > Hi,
>
> > I'm new to Python and I'm having some problems with getting different
> > results from my script when I run it from IDLE versus just double-
> > clicking the .py file and having it run through the command line.
> > Basically, my script reads some CSV files, assembles a text files,
> > then uploads that test file to an ftp site. When I run the script from
> > IDLE, everything works fine. But when it runs from the command line,
> > the file that gets uploaded is empty. It seems that it is creating a
> > new file to upload, rather than using the existing file (or at least
> > that's what I think is going on.) Below is my script. I apologize for
> > any ugly code. Thanks for your help.
>
> > import sys,os,linecache,csv,ftplib,time
>
> > starttime = time.time()
>
> > #*******
> > #Summary file assembling
> > #*******
> > currentdir = "//folder/"
>
> > reg_sum = open('reg_sum.txt','w')
> > reg_sum.close
>
> > for files in os.listdir(currentdir):
> >     reg_file = csv.reader(open(currentdir + files))
> >     for row in reg_file:
> >         reg_sum = open('reg_sum.txt','a')
> >         reg_sum.write(",".join(row) + ',\n')
>
> > reg_sum.close
>
> > #*******
> > #Summary file processing
> > #*******
> > coordList = [
> >     ["F10",40.0053,-75.0927],
> >     ["T10",40.0272,-75.1123],
> >     ["D22",39.9811,-75.0998],
> >     ["P02",40.0437,-75.0217],
> >     ["D68",39.9203,-75.1388],
> >     ["D51",39.9534,-75.1405],
> >     ["S43",39.9217,-75.2275],
> >     ["S33",39.9360,-75.2077],
> >     ["S42A",39.9215,-75.1937],
> >     ["S05",39.9617,-75.1782],
> >     ["T14",40.0165,-75.1077]]
>
> > coordList_index =
> > ["F10","T10","D22","P02","D68","D51","S43","S33","S42A","S05","T14"]
> > #coordList_index is a list containing
>
> > in_text = open('reg_sum.txt','r')
>
> > out_text = open('reg_out.txt','w')
> > out_text.close
>
> > out_text = open('reg_out.txt','a')
>
> > for line in in_text:
> >     split_line = line.split(',')
> >     if (split_line[0]) in coordList_index:
> >        i = coordList_index.index(split_line[0])
> >        coords = str(coordList[i][1]) + "," + str(coordList[i][2])
> >        last_update = str(split_line[2])
> >     print str(split_line[0])
> >     if split_line[1] == "1":
> >        out_text.write(split_line[0] + "<br>,Test1: " + last_update +
> > "," + coords + ",1" + "\n")
> >     elif split_line[1] == "0":
> >        out_text.write(split_line[0] + "<br>,Test2.<br>Last updated: "
> > + last_update + ","  + coords + ",0" + "\n")
> >     else:
> >        out_text.write(split_line[0] + "<br>,No data.," + coords +
> > "\n")
>
> > in_text.close
>
> > ###*******
> > ###Uploads file via FTP
> > ###*******
> > s = ftplib.FTP('ftp.blah123.org,'user','pass')
>
> > f.open('reg_out.txt','r')
> > s.storlines('STOR reg_out.txt', f)
>
> > f.close()
> > s.quit()
>
> > print  "Processed in " + str(((time.time() - starttime) / 60) *60) + "
> > seconds!" #prints elapsed time
>
> You never closed your file.
>
> file_object.close
> <built-in method close of file object at 0x00BA6CC8>
>
> try adding '()' at the end of your close calls.
> As to why it differs, I can't think offhand why it wouldn't.

wow. thank you so much. it was the '()'. I now get consistent results.
In all the sample code I saw, I never saw the '()' after close. thanks
again

- casey



More information about the Python-list mailing list