Open main menu

CDOT Wiki β

Changes

OPS435 Python3 Lab 2

38 bytes added, 13:38, 12 September 2019
PART 2 - Command line arguments
print(len(sys.argv)) # tells us the number of command line arguments the user provide from a command shell
sys.exit() # will immediately end the running Python script, ignoring the remaining lines in the Python script
</source><br>Instead of using the '''input()''' function to prompt the user for data, we can use the list object '''sys.argv''' to capture the data items provided at the command line when a Python script is being executed. The list object '''sys.argv''', when used within your Python script can store the following:<ul><li>'''sys.argv''' - stores all argument items as string objects</li><li>'''sys.argv[0]''' - stores the name of the script/program</li><li>'''sys.argv[1]''' - stores the first argumentas a string object</li><li>'''sys.argv[2]''' - stores the second argumentas a string object</li><li>etc...</li><li>'''len(sys.argv)''' - gives the number of arguments</li></ul><br>
:#Create a new script called '''~/ops435/lab2/showargs.py''' and add the following content:<source lang="python">
#!/usr/bin/env python3
1,760
edits