Lecture 9 Revision Flashcards
What is the sys module?
The sys module provides access to some variables and functions that interact with the Python interpreter.
Need to import it using command import sys
What is sys.argv?
The sys.argv list contains command-line arguments passed to the script.
The first element of the sys.argv list is sys.arg[0] which is the script name. The subsequent elemts in the sys.argv list are the arguments
Can use sys.argv to get the input and output file names from the command line.
For example in a script use input_file_name = sys.argv[1]
output_file_name = sys.srgv[2]
then you can type the input and output file names in the command line after the python scriptname.py
Creating a usage function
We can create a usage function to tell us if a script is being used incorrectly. The usage function can be created to be called if the script is being used incorrectly
What does sys.exit do?
sys.exit tells the script to stop running. No further commands will be executed.