1

I have been trying the following, as suggested by many post, but I am still getting

ValueError: invalid literal for int() with base 10: 'gap_interval.py'. What am I missing?

import sys 

if __name__ == '__main__':
    n = 4
    a = []
    for i in sys.argv:
        print int(i.strip())
2
  • 1
    gap_interval.py is not a string. It is also part of sys.argv which it's telling you. Commented Apr 9, 2014 at 23:08
  • right, it's sys.argv[0]. Thank you!! Commented Apr 9, 2014 at 23:10

2 Answers 2

4

Python sys.argv always contains the filename as the first argument:

>>> python test.py 1 2 3
>>> sys.argv
['test.py', '1', '2', '3']

If you want to convert it to int, you can do:

nums = map(int, sys.argv[1:])

>>> nums
[1,2,3]
Sign up to request clarification or add additional context in comments.

Comments

3

You need to skip sys.argv[0], which is the script's name:

for i in sys.argv[1:]:
    # ...

Comments

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.