2.3K
The main use I’ve found for the Python sys module is allowing command-line arguments to be made to a script. Here is an example of how it looks:
import sys
if len(sys.argv) == 2:
input_file = sys.argv[1]
else:
print "Please input a command-line argument specifying the file"
This script checks that 2 command-line arguments had been passed to the program before assigning the value sys.argv[1] to a variable. We check for two command-line arguments because the first one (sys.argv[0]) is the name of the Python script currently being executed.