Re: [Tutor] Command line args

2007-04-14 Thread Alan Gauld

Teresa Stanton [EMAIL PROTECTED] wrote

 No one suggested this.  That's great! Wish I had seen it sooner. 
 Thanks,
 I'll put that in my notebook for further use later.

Note that Fileinput is used to iterate over a (set of) file line by 
line,
it doesn't read the entire file into a string as in your original
question. You can get round that by using join() to join the
lines togvether after reading.

Also there are no prompts to stdin if that matters.

HTH,

Alan G.


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Command line args

2007-04-13 Thread Daniel Yoo
Hi Teresa,

Has anyone on this thread already suggested the 'fileinput' module?  From 
what I understand, what 'fileinput' does is exactly what you're asking 
from:

 http://mail.python.org/pipermail/tutor/2007-April/053669.html

Here's documentation on 'fileinput':

 http://www.python.org/doc/lib/module-fileinput.html


Good luck!
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Command line args

2007-04-13 Thread Teresa Stanton
No one suggested this.  That's great! Wish I had seen it sooner.  Thanks,
I'll put that in my notebook for further use later.

 

-Original Message-
From: Daniel Yoo [mailto:[EMAIL PROTECTED] 
Sent: Friday, April 13, 2007 5:51 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: RE: Command line args

Hi Teresa,

Has anyone on this thread already suggested the 'fileinput' module?  From
what I understand, what 'fileinput' does is exactly what you're asking
from:

 http://mail.python.org/pipermail/tutor/2007-April/053669.html

Here's documentation on 'fileinput':

 http://www.python.org/doc/lib/module-fileinput.html


Good luck!


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Command line args

2007-04-10 Thread Alan Gauld
Kirk Bailey [EMAIL PROTECTED] wrote 

 Try:
 filename=sys.arv[1]
 except Exception, e:

This still doesn't help for the problem where a 
different exception is raised.It really does need to be

try: filename = sys.argv[1]:
except IndexError:


 if filename='':
 filename='foo' # define a default value
 else:
 if foo: # detect one likely error
 foobarcode
 else:

This is invalid syntax it would need to be a chain of if/elif/else

 another idea is simply detect that there IS a argument;
 
 if sys.argv[1];
 filename=sys.argv[1]
 ...
 which avoids try altogether. Somehow I kinda like this way more.

This would still throw an exception if argv[1] doesn't exist 
because the code still tries to access non existent data. 
You cannot get away without using the try/except here 
unless you check the length of argv:

if len(sys.argv)  1:
filename = sys.argv[1]

Now you can check whether filename is valid or not.

HTH,

-- 
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] Command line args

2007-04-09 Thread Luke Paireepinart
Kirk Bailey wrote:
 Teresa Stanton wrote:
   
 If one argument to a script is provided I am to take the input from it.  
 I figure that is presented like this:

 filename = sys.argv[1]
 
 Try:
   
the 'try' keyword is not capitalized in Python.
   filename=sys.arg[1]
 except exception, E:
   
you should only catch the exception you expect, so you don't 
accidentally silence an unrelated error.
so
except IndexError:
because you're trying to index into a list that might not have 2 or more 
elements.
   filename='FooBar'
   
You said you wanted the input from standard input, so just put a 
raw_input here.

so the new code is:

try: filename = sys.argv[1]
except IndexError: filename = raw_input(Prompt: )

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


Re: [Tutor] Command line args

2007-04-09 Thread Kirk Bailey
ok, try this:

Try:
filename=sys.arv[1]
except Exception, e:
if filename='':
filename='foo'  # define a default value
else:
if foo: # detect one likely error
foobarcode
else:
if bar: # detect another one
morefoobarcode
else:   # final catchall for things you
# did not anticipate
Print 'how the heck did you accomplish this?!? I QUIT~!
sys.exit(13)

i has something vauely like this in the wiki on the slab right now, 
except it was addressing the query string. other than that, same problem.

another idea is simply detect that there IS a argument;

if sys.argv[1];
filename=sys.argv[1]
if condition:
do something
else:
do this instead
else:
filename=foo

which avoids try altogether. Somehow I kinda like this way more.



Luke Paireepinart wrote:
 Kirk Bailey wrote:
 Teresa Stanton wrote:
  
 If one argument to a script is provided I am to take the input from 
 it.  I figure that is presented like this:

 filename = sys.argv[1]
 
 Try:
   
 the 'try' keyword is not capitalized in Python.
 filename=sys.arg[1]
 except exception, E:
   
 you should only catch the exception you expect, so you don't 
 accidentally silence an unrelated error.
 so
 except IndexError:
 because you're trying to index into a list that might not have 2 or more 
 elements.
 filename='FooBar'
   
 You said you wanted the input from standard input, so just put a 
 raw_input here.
 
 so the new code is:
 
 try: filename = sys.argv[1]
 except IndexError: filename = raw_input(Prompt: )
 
 HTH,
 -Luke
 
 

-- 
Salute!
-Kirk Bailey
   Think
  +-+
  | BOX |
  +-+
   knihT

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


Re: [Tutor] Command line args

2007-04-08 Thread Kirk Bailey


Teresa Stanton wrote:
 If one argument to a script is provided I am to take the input from it.  
 I figure that is presented like this:
 
 filename = sys.argv[1]
Try:
filename=sys.arg[1]
except exception, E:
filename='FooBar'

 data = open(filename).read()
 
 But, if none are provided, input should come from standard input.  How 
 do I write that code?
 
 TY
 
 
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
 
 
 
 No virus found in this incoming message.
 Checked by AVG Free Edition.
 Version: 7.5.446 / Virus Database: 269.0.0/750 - Release Date: 4/6/2007 9:30 
 PM

-- 
Salute!
-Kirk Bailey
   Think
  +-+
  | BOX |
  +-+
   knihT

Fnord.

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


Re: [Tutor] Command line args

2007-04-07 Thread Alan Gauld

Teresa Stanton [EMAIL PROTECTED] wrote

 If one argument to a script is provided I am to take the input from 
 it.

OK This sounds like a homework so I can't give you a direct
answer but only some things to consider.

 I figure that is presented like this:

 filename = sys.argv[1]
 data = open(filename).read()

So far so good but how will you know whether there is
anyting in sys.argv to read?

 But, if none are provided, input should come from standard input.

Standard input is where you normally get input in interactive 
programs.
How do you normally get a user to giove you information?

You'll find more about receiving input from users in the
Talking to the User topic of my tutorial.

-- 
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] Command line args

2007-04-07 Thread Andreas Kostyrka
* Teresa Stanton [EMAIL PROTECTED] [070407 18:52]:
If one argument to a script is provided I am to take the input from it.  I
figure that is presented like this:
 
filename = sys.argv[1]
data = open(filename).read()
 
But, if none are provided, input should come from standard input.  How do
I write that code?

if len(sys.argv)  1:
 fp = file(sys.argv[1])
else:
 fp = sys.stdin
data = fp.read()
if fp is not sys.stdin:
 fp.close()

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