Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

191 bytes added, 19:15, 25 May 2017
PART 2 - Arguments
print(sys.argv) # tells us our arguments
sys.exit() # will immediately end our script when run, ignoring the remaining lines in the Python script
</source>Note that the '''sys.exit()''' function will be useful later in this lab, make sure you write it down in your notes.<br><br>Instead of using the input() function to prompt the user for data, we can use the sys function to obtain data from running the Python script instead. The '''sys.argv''' function (within the running Python script) stores all arguments, '''sys.argv[0]''' stores the name of the script/program, '''sys.argv[1]''' store the first argument, '''sys.argv[2]''' stores the second argument, etc.<br><br>
:#Create a new script called '''~/ops435/lab2/showargs.py''' and add the following content:<source>
#!/usr/bin/env python3
run showargs.py argument1 argument2 argument3
</source>
:#The '''sys.argv''' contains all arguments, '''sys.argv[0]''' contains the name of the script/program, '''sys.argv[1]''' contains the first argument, '''sys.argv[2]''' contains the second argument, etc.
''' Practice Using Arguments '''
13,420
edits