Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Different results when running script from IDLE versus Command Line

Reply
Thread Tools

Different results when running script from IDLE versus Command Line

 
 
Casey T
Guest
Posts: n/a
 
      03-12-2008
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","S 42A","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
 
Reply With Quote
 
 
 
 
Chris
Guest
Posts: n/a
 
      03-12-2008
On Mar 12, 8:10 pm, Casey T <(E-Mail Removed)> 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","S 42A","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.
 
Reply With Quote
 
 
 
 
Casey T
Guest
Posts: n/a
 
      03-13-2008
On Mar 12, 5:28*pm, Chris <(E-Mail Removed)> wrote:
> On Mar 12, 8:10 pm, Casey T <(E-Mail Removed)> 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","S 42A","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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
DLL search path, IDLE versus command line h112211@gmail.com Python 0 08-02-2006 06:26 AM
PyGreSQL, DLL search path, IDLE versus python command line h112211@gmail.com Python 0 08-01-2006 12:30 PM
newbie running IDLE with command line arguments crypto Python 4 05-19-2005 04:09 PM



Advertisments