Intro

The sys module in Python provides various functions and variables that are used to manipulate different parts of the Python runtime environment. It allows operating on the interpreter as it provides access to the variables and functions that interact strongly with the interpreter.

Eg: sys.version() - This shows how the sys module interacts with the interpreter.

Untitled

Input and Output using sys

These are File objects used by the interpreter for standard input, output and errors:

sys.stdin

sys.stdout

sys.stderr

sys.argv

sys.argv returns a list of command line arguments passed to a Python script. The item at index 0 in this list is always the name of the script. The rest of the arguments are stored at the subsequent indices. Eg: -

import sys
print("You entered: ",sys.argv[1], sys.argv[2], sys.argv[3])

Output

Output

sys.exit

sys.exit(-1) causes the script to exit back to either the Python console or the command prompt. This is generally used to safely exit from the program in case of generation of an exception. Generally, 0 denotes successful execution and any other number (usually 1 or -1) means something broke.

sys.maxsize

Returns the largest integer a variable can take.

Eg: -

>> import sys
>> sys.maxsize
9223372036854775807