0

I'm tryting to use one integer as positional argument in argparse. Given is the following complete python script (taken from this tutorial):

   #!/usr/bin/env python
   import argparse
   parser = argparse.ArgumentParser()
   parser.add_argument("square", help="display a square of a given number",
                    type=int)
   args = parser.parse_args()
   print args.square**2

When I call this script as

   > script.py 3

I get as a result

   error: argument square: invalid int value: 'C:\\home\\dev\\freqana\\3'

argparse clearly expects a filesystem path here, but why? The type is for sure set to int.

1 Answer 1

1

The code is correct. I ran your program on python 2.7 and got the expected answer:

> script.py 3
9

Are you running the right code? cat (linux) or type (windows) script.py to make sure you have the latest, and then run under python explicitly to make sure you aren't picking up a different script.py on your path (python script.py 3)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. My fault: The system did indeed picked up something different. With your assurance that the code is right, it was easy for me to find the culprit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.