program works as a script, not interactively

Lee Harr missive at frontiernet.net
Mon May 17 17:03:21 EDT 2004


On 2004-05-17, Philip Carter <pcarter at 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.




More information about the Python-list mailing list