Re: problem with sys import argv

Discussion in 'Python' started by Dave Angel, Mar 26, 2013.

  1. Dave Angel

    Dave Angel Guest

    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
     
    Dave Angel, Mar 26, 2013
    #1
    1. Advertising

  2. 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.
     
    Thomas 'PointedEars' Lahn, Mar 26, 2013
    #2
    1. Advertising

  3. Dave Angel

    Dave Angel Guest

    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
     
    Dave Angel, Mar 26, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,597
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    5,962
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  4. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    909
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  5. Mark Lawrence

    Re: problem with sys import argv

    Mark Lawrence, Mar 26, 2013, in forum: Python
    Replies:
    0
    Views:
    125
    Mark Lawrence
    Mar 26, 2013
Loading...

Share This Page