By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

program works as a script, not interactively

P: n/a
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

--
pc*****@uk.ibm.com
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Philip Carter wrote:
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

Jul 18 '05 #2

P: n/a
On 2004-05-17, Philip Carter <pc*****@uk.ibm.com> wrote:
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 ...
if True:

.... 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.

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.