Changes

Jump to: navigation, search

OPS435 Python3 Lab 2

24 bytes added, 13:29, 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''' <b>list object</b> to capture the data as item provided at the command line when execution a result of running the Python script with arguments. The list object '''sys.argv''', when used within your Python script can store the following:<ul><li>'''sys.argv''' - stores all argument itemsas string objects</li><li>'''sys.argv[0]''' - stores the name of the script/program</li><li>'''sys.argv[1]''' - stores the first argument</li><li>'''sys.argv[2]''' - stores the second argument</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

Navigation menu