473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

program works as a script, not interactively

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
2 1457
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rajarshi Guha | last post by:
Hi, I have some code that runs an interactive program via popen3(). The program generates output which is then used later on. My script looks like this: o,i,e =...
6
by: David Klaffenbach | last post by:
Is there a way from within a python script to cause the interpreter to be in interactive mode after the script finishes? so that if I run: myscript.py it will always execute as if I had...
11
by: tdi | last post by:
Ok, stupid question for the day. I'm reading the interview with Steve Moret and he says: "Once a lot of scripts started going in we knew there was no way we could back out of using Python." I'm...
2
by: qwweeeit | last post by:
Hi all, using Python 2.4 under Linux (SUSE 9.3) I was developping a script to get various lists related with DCOP. In interactive Python all is working correctly: import pcop # application's...
3
by: Tuang | last post by:
I'd like to create my own mini "IDE" for working with several programming languages that provide interactive "toplevel" command line interpreters, such as Python, Ruby, Lisp, Scheme, OCaml, etc....
7
by: Leo Breebaart | last post by:
I have another question where I am not so much looking for a solution but rather hoping to get some feedback on *which* solutions people here consider good Pythonic ways to approach a issue. ...
9
by: Susan Rice | last post by:
I'm running a simple win32 console application and I want to impliment a "Press any key to continue", so I print that prompt, and then what's the easiest way to impliment reading any key? Do I use...
17
by: LittleRob | last post by:
I'm having problems using Windows Task scheduler (or AT or SCHTASKS) to run a VB.NET program unattended. I'm able to reduce it to some really simple code that still fails My program has a Sub...
2
by: LayneMitch via WebmasterKB.com | last post by:
Hello. I posted this earlier with the full code of the JavaScript file I'm trying to open. I'm posting this again, because the file is not the problem. I seem to be having a 'windows' problem...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.