Re: [Tutor] input and output files from terminal

2008-04-17 Thread Alan Gauld

"Brain Stormer" <[EMAIL PROTECTED]> wrote

> Thanks for the file direction method but how do I get the names of 
> the files
> so I can use it inside my program.

I'm not sure I understand.

Thepoint of using redirection is that your code doesn't need to know
anything about the files, it just reads/writes to/from stdin/stdout.

The OS connects the bits together at runtime.

>> Easier to use file redirection:
>>
>> python test.py < inputfile > outputfile

So you can provide any name you like for inputfile
and outputfile when you execute the program.

If you want the program to read/write to a specific file then
use the standard open()/read/write functions

Or am I missing something?

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] input and output files from terminal

2008-04-17 Thread Brain Stormer
Actually, Let me take that back with the raw_input comment since it is not
the idle issue but I didn't want to the program to be interactive so I
didn't want to wait for someone to put the information and press enter.
Thanks for the file direction method but how do I get the names of the files
so I can use it inside my program.

On Mon, Apr 14, 2008 at 2:57 PM, Alan Gauld <[EMAIL PROTECTED]>
wrote:

>
> "Brain Stormer" <[EMAIL PROTECTED]> wrote
>
> >I have a python program which works fine when run using idle but I
> >would
> > like call the program from the terminal.
> >
> > python test.py -i inputfile -o outputfile
>
> Easier to use file redirection:
>
> python test.py < inputfile > outputfile
>
> The -i flag in particular might conflict with Pythons own -i option.
>
> > I tried with raw_input but that only works in idle.
>
> What makes you think that?
> raw_input is the normal way to get input from stdin.
>
> Can you explain what you did and what the result was?
> Perhaps with a short code example?
>
> Try this for example:
>
> 
>
> name = raw_input('What's your name? ')
> print "hello", name
>
> 
>
> That can be run in IDLE or in a command window
> or using file redirection as described above.
>
> Tell us how you get on...
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] input and output files from terminal

2008-04-14 Thread Alan Gauld

"Brain Stormer" <[EMAIL PROTECTED]> wrote

>I have a python program which works fine when run using idle but I 
>would
> like call the program from the terminal.
>
> python test.py -i inputfile -o outputfile

Easier to use file redirection:

python test.py < inputfile > outputfile

The -i flag in particular might conflict with Pythons own -i option.

> I tried with raw_input but that only works in idle.

What makes you think that?
raw_input is the normal way to get input from stdin.

Can you explain what you did and what the result was?
Perhaps with a short code example?

Try this for example:



name = raw_input('What's your name? ')
print "hello", name



That can be run in IDLE or in a command window
or using file redirection as described above.

Tell us how you get on...


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] input and output files from terminal

2008-04-14 Thread Brain Stormer
I used the optparse module since that is exactly what I wanted.  Here is my
code:

import sys
from optparse import OptionParser
import os

parser = OptionParser()
parser.add_option("-i", "--input", dest="infile",
help="input FILE to convert", metavar="FILE")
parser.add_option("-o", "--output", dest="outfile",
help="output FILE to convert to", metavar="FILE")
(options, args) = parser.parse_args()

if not os.path.isfile(options.infile):
print "Input files does not exit...Quitting"
sys.exit()
elif os.path.isfile(options.outfile):
print "Output file exists, Remove it first"
sys.exit()

Thanks everyone.


On Mon, Apr 14, 2008 at 1:14 PM, <[EMAIL PROTECTED]> wrote:

> look at the OptParse module, with this u can easily realize such things.
> http://docs.python.org/lib/module-optparse.html
>
>
> On Mon, Apr 14, 2008 at 12:55:07PM -0400, Brain Stormer wrote:
> > I have a python program which works fine when run using idle but I would
> > like call the program from the terminal.
> >
> > python test.py -i inputfile -o outputfile
> >
> > I tried with raw_input but that only works in idle.  Can this be
> achieved?
> > Thanks
>
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] input and output files from terminal

2008-04-14 Thread bhaaluu
On Mon, Apr 14, 2008 at 12:55 PM, Brain Stormer <[EMAIL PROTECTED]> wrote:
> I have a python program which works fine when run using idle but I would
> like call the program from the terminal.
>
> python test.py -i inputfile -o outputfile
>
> I tried with raw_input but that only works in idle.  Can this be achieved?
>  Thanks
>
> ___
>  Tutor maillist  -  Tutor@python.org
>  http://mail.python.org/mailman/listinfo/tutor

Please see:
http://www.faqs.org/docs/diveintopython/kgp_commandline.html

>From the book: Dive into Python.
Source code examples from the book:
http://diveintopython.org/download/diveintopython-examples-4.1.zip

Happy Programming!
-- 
b h a a l u u at g m a i l dot c o m
Kid on Bus: What are you gonna do today, Napoleon?
Napoleon Dynamite: Whatever I feel like I wanna do. Gosh!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] input and output files from terminal

2008-04-14 Thread linuxian iandsd
i think you need to try :

cat input.txt | /usr/bin/python test.py >output.txt
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] input and output files from terminal

2008-04-14 Thread v2punkt0
look at the OptParse module, with this u can easily realize such things.
http://docs.python.org/lib/module-optparse.html


On Mon, Apr 14, 2008 at 12:55:07PM -0400, Brain Stormer wrote:
> I have a python program which works fine when run using idle but I would
> like call the program from the terminal.
> 
> python test.py -i inputfile -o outputfile
> 
> I tried with raw_input but that only works in idle.  Can this be achieved?
> Thanks

> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor