Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: problem with sys import argv

Reply
Thread Tools

Re: problem with sys import argv

 
 
Dave Angel
Guest
Posts: n/a
 
      03-26-2013
On 03/26/2013 01:26 PM, leonardo selmi wrote:
> hi python community,
>
> i wrote the following programm:
>
> from sys import argv
>
> script, userName = argv
> prompt = '> '
>
> print 'hi %s, i am the %s script' % (userName, script)
> print "i'd like to ask you a few questions."
> print 'do you like me %s' % userName
> likes = raw_input(prompt)
>
> print "where do you live %s?" % userName
> lives = raw_input(prompt)
>
> print 'what kind of computer do you have?'
> computer = raw_input(prompt)
>
> print """
> alright so you said %r about liking me.
> you live in %r. not sure where that is.
> and you have a %r computer. nice
> """ % (likes, lives, computer)
>
> and i got the following error: Traceback (most recent call last):
> File "/var/folders/89/84z7tw3d3rv39gny3n2p963m0000gn/T/pythonInTerm.GUF6PWCM", line 3, in <module>
> script, userName = argv
> ValueError: need more than 1 value to unpack
>
> what can i do?
>
> thanks!
>


Since the script takes a mandatory argument, run it with one.

python myscript.py Dave

Better would be to change the script to check len(argv) for exactly 2,
and tell the user how he should have run it.



--
DaveA
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-26-2013
Dave Angel wrote:

> Since the script takes a mandatory argument, run it with one.
>
> python myscript.py Dave
>
> Better would be to change the script to check len(argv) for exactly 2,
> and tell the user how he should have run it.


I would use argparse.ArgumentParser instead.

<http://docs.python.org/dev/library/argparse.html>

--
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      03-26-2013
On 03/26/2013 04:12 PM, Thomas 'PointedEars' Lahn wrote:
> Dave Angel wrote:
>
>> Since the script takes a mandatory argument, run it with one.
>>
>> python myscript.py Dave
>>
>> Better would be to change the script to check len(argv) for exactly 2,
>> and tell the user how he should have run it.

>
> I would use argparse.ArgumentParser instead.
>
> <http://docs.python.org/dev/library/argparse.html>
>


As would I. But that would be out of proportion of the rest of the
code, and therefore presumably beyond the needs or interest of the OP.

--
DaveA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: problem with sys import argv Mark Lawrence Python 0 03-26-2013 05:42 PM
comp.sys.mac.games.misc,comp.sys.ibm.pc.hardware.misc,comp.mail.pegasus-mail.ms-windows,comp.databases.paradox,comp.dcom.sys.cisco . Cisco 0 06-19-2004 07:48 AM
difference(s) between char** argv and char* argv[] David C Programming 10 09-15-2003 06:58 AM
char **argv vs. char* argv[] Bret C Programming 21 09-03-2003 03:24 AM
sys.argv[0] - 'module' object has no attribute 'argv' =?ISO-8859-1?Q?Thomas_N=FCcker?= Python 0 06-30-2003 02:07 PM



Advertisments