program works as a script, not interactively

P

Philip Carter

Hello there,

I am using:
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32

on a win2k box.

I have the following piece of python code:

inputTmpFileOnSTAXServer='c:/temp/phil.txt'
outputTmpFileOnSTAXServer='c:/temp/philOut.txt'
import re # import regular expression module
regExp=re.compile('(\[.+\]) Tests run: (\d+), Failures: (\d+)') #
compile the regular expression
inFile=open(inputTmpFileOnSTAXServer) # open the input file for reading
outFile=open(outputTmpFileOnSTAXServer,'w') # open the output file for
writing
aLine=inFile.readline() # read a line
while aLine != "": # loop round until EOF
match=regExp.search(aLine) # search for regExp in aLine
if match: # if there is a match
aDir = match.group(1) # store the dir section of the line
attempted = match.group(2) # store the number of tests attempted

attemptedInt = int(attempted) # convert it into an int
failed = match.group(3) # store number of failures
failedInt = int(failed)
passedInt = attemptedInt - failedInt # calculate the number
passed
passedStr = str(passedInt) # convert it into a string
outLine=aDir+'\n'+attempted+' tests were
attempted\n'+passedStr+' test(s) passed\n' # create summary line in
correct format
outFile.write(outLine) # write out the summary line
aLine=inFile.readline() # read a line
outFile.close() # close the output file
inFile.close()

When I save in a file - terry.py and run it:
python terry.py
it works fine.

However, if I try and paste it into an interactive python window, I get
the following error:

.... outFile.close() # close the output file
File "<stdin>", line 14
outFile.close() # close the output file
^
SyntaxError: invalid syntax

Any idea why this is?

Thanks,

Phil
 
B

Bruno Desthuilliers

Philip said:
Hello there,

I am using:
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32

on a win2k box.

I have the following piece of python code:
(snip code)
When I save in a file - terry.py and run it:
python terry.py
it works fine.

However, if I try and paste it into an interactive python window, I get
the following error:

... outFile.close() # close the output file
File "<stdin>", line 14
outFile.close() # close the output file
^
SyntaxError: invalid syntax

Any idea why this is?

Probably a problem with indentation... I sometime have this kind of
problem when copy/pasting pieces of code in the interpreter.

Bruno
 
L

Lee Harr

Hello there,

I am using:
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32

on a win2k box.

I have the following piece of python code:

inputTmpFileOnSTAXServer='c:/temp/phil.txt'
outputTmpFileOnSTAXServer='c:/temp/philOut.txt'
import re # import regular expression module
regExp=re.compile('(\[.+\]) Tests run: (\d+), Failures: (\d+)') #
compile the regular expression
inFile=open(inputTmpFileOnSTAXServer) # open the input file for reading
outFile=open(outputTmpFileOnSTAXServer,'w') # open the output file for
writing
aLine=inFile.readline() # read a line
while aLine != "": # loop round until EOF
match=regExp.search(aLine) # search for regExp in aLine
if match: # if there is a match
aDir = match.group(1) # store the dir section of the line
attempted = match.group(2) # store the number of tests attempted

attemptedInt = int(attempted) # convert it into an int
failed = match.group(3) # store number of failures
failedInt = int(failed)
passedInt = attemptedInt - failedInt # calculate the number
passed
passedStr = str(passedInt) # convert it into a string
outLine=aDir+'\n'+attempted+' tests were
attempted\n'+passedStr+' test(s) passed\n' # create summary line in
correct format
outFile.write(outLine) # write out the summary line
aLine=inFile.readline() # read a line
outFile.close() # close the output file
inFile.close()

When I save in a file - terry.py and run it:
python terry.py
it works fine.

However, if I try and paste it into an interactive python window, I get
the following error:

... outFile.close() # close the output file
File "<stdin>", line 14
outFile.close() # close the output file
^
SyntaxError: invalid syntax

Any idea why this is?


Try typing something simple at the prompt ...
.... print 'true'
....
true

and notice that to get out of the if statement, you
have to press ENTER twice.

In any event, you will probably be better off putting
your code in to a file and importing it.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top