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.
These are File objects used by the interpreter for standard input, output and errors:
sys.stdin
stdin
is used for all interactive input (including calls to input()
).sys.stdout
stdout
is used for the output of print()
and for the prompts of input()
.
sys.stdout.write() and sys.stdout.flush()
import sys
sys.stdout.write('.') # similar to print()
**sys.stdout.flush()**
# to ensure stdout is flushed immediately, similar to print('a', flush=True)
sys.stderr
stderr
.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
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.
Returns the largest integer a variable can take.
Eg: -
>> import sys
>> sys.maxsize
9223372036854775807