Skip to content

Instantly share code, notes, and snippets.

@BlogBlocks
Last active December 19, 2017 00:27
Show Gist options
  • Select an option

  • Save BlogBlocks/f77d7ecf17a8f92d6a036393071aa60f to your computer and use it in GitHub Desktop.

Select an option

Save BlogBlocks/f77d7ecf17a8f92d6a036393071aa60f to your computer and use it in GitHub Desktop.
Some scripts to learn how to use sys.argv
ArgTest1.py
#!/usr/local/bin/python
import sys
a = sys.argv[1]
print a
-- tryit:
!python ArgTest1.py 456 721
456
# result:
------------------
ArgTest2.py
#!/usr/local/bin/python
import sys
a = sys.argv[2]
print a
#or
print sys.argv[2]
# This will print twice to show the argv may be used directly or assigned to a variable.
---tryit:
#ArgTest2.py script executes " print a" AND " print sys.argv[2] "
!python ArgTest2.py 456 721 34 655
#result:
721
721
------------------
ArgTest3.py
#!/usr/local/bin/python
import sys
# notice the colon after the 1. sys.argv[1:] - turns into a list
a,b,c,d,e,f = sys.argv[1:]
print sys.argv[1:]
#or identify the elements and get the unformated results
#to work correctly all 6 elements must be supplied
print a,b,c,d,e,f
-- tryit:
!python ArgTest3.py 456 721 45 2827 234 23
#result:
['456', '721', '45', '2827', '234', '23']
456 721 45 2827 234 23
------------------
ArgInteger.py
#!/usr/local/bin/python
import sys
# notice the colon after the 1. sys.argv[1:]
a,b = sys.argv[1:]
print a+b
-- tryit:
!python ArgInteger.py 456 721
#result
456721
------------------
%%writefile ArgInteger03.py
#!/usr/local/bin/python
import sys
A,B,C = sys.argv[1:]
A=int(A);C=int(C)
print A+C
-- tryit:
!python ArgInteger03.py 10 2 10
#result:
20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment