Different results when running script from IDLE versus Command Line

Discussion in 'Python' started by Casey T, Mar 12, 2008.

  1. Casey T

    Casey T Guest

    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[1]) + "," + str(coordList[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
    Casey T, Mar 12, 2008
    #1
    1. Advertising

  2. Casey T

    Chris Guest

    Re: Different results when running script from IDLE versus CommandLine

    On Mar 12, 8:10 pm, Casey T <> 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[1]) + "," + str(coordList[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.
    Chris, Mar 12, 2008
    #2
    1. Advertising

  3. Casey T

    Casey T Guest

    Re: Different results when running script from IDLE versus CommandLine

    On Mar 12, 5:28 pm, Chris <> wrote:
    > On Mar 12, 8:10 pm, Casey T <> 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[1]) + "," + str(coordList[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
    Casey T, Mar 13, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. crypto
    Replies:
    4
    Views:
    4,499
  2. Replies:
    0
    Views:
    597
  3. Replies:
    0
    Views:
    349
  4. Paul Butcher
    Replies:
    12
    Views:
    692
    Gary Wright
    Nov 28, 2007
  5. John Wu
    Replies:
    1
    Views:
    123
    Jean-Julien Fleck
    Feb 24, 2010
Loading...

Share This Page