Functions and file objects in Python sys module

sys stands for the system. The module consists of various functions and variables that help the programmer to manipulate Python's run-time and compile-time environment settings. It directly interacts with and operates the Python Interpreter.

The first step in using the functionality of the sys module is to import the module into the program using the statement:

In this tutorial, we'll cover these contents:

  1. Basic functions in the module
  2. Standard file objects of the module:
    • stdin
    • stdout
    • stderr
  3. argv

Basic functions and variables in the sys module:

sys.copyright()Displays the copyright of the Python version installed on the operating system:
Example:
import sys
print(sys.version)
Output:
3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]
sys.getrecursionlimit()
sys.setrecursionlimit()
  • Returns the recursion depth
  • Updates the recursion depth to the given value.

Example:
import sys
print(sys.getrecursionlimit())
sys.setrecursionlimit(4000)
print(sys.getrecursionlimit())
Output:
3000
4000
sys.getcheckinterval()
sys.setcheckinterval()
These two functions are replaced with:
1.1. Sys.setswitchinterval()
2.2. Sys.getswitchinterval()
3.From Python 3
  1. Returns the time in seconds the interpreter checks for executable signal handlers amid code execution. (Threading) in other words, thread switch interval.
  2. Updates the time interval with the given value.
  3. Example:
    import sys
    print(sys.getcheckinterval())
    sys.setcheckinterval(300)
    print(sys.getcheckinterval())
    Output:
    100
    300
    DeprecationWarning: sys.getcheckinterval() and sys.setcheckinterval() are deprecated. Use sys.getswitchinterval() instead.
    Print(sys.getcheckinterval())
sys.executableReturns the path of the interpreter's executable binary, which runs all the python scripts we write.
Example:
import sys
print(sys.executable)
Output:
C:\ProgramData\Anaconda3\python.exe
sys.pathReturns the list of paths the interpreter searches for when we import a module in our code.
Example:
import sys
print(sys.path)
Output:
['C:\\ProgramData\\Anaconda3\\python38.zip', 'C:\\ProgramData\\Anaconda3\\DLLs'…]
sys.platform()Returns the platform of the system the code is running in without the major version.
1.Windows: win32
2.macOS: darwin
Example:
import sys
print(sys.platform)
Output:
win32
sys.version()Returns the version number of Python running on the system along with more details like the build number and the compiler.
Example:
import sys
print(sys.version)
Output:
3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]
sys.api_version()Returns the C api version the interpreter is using. This function is mostly used for debugging a python code with extended modules.
Example:
import sys
print(sys.api_version)
Output:
1013
sys.winver()Example:
import sys
print(sys.winver)
Output:
3.8
sys.getwindowsversion()Returns the windows version of the system.
Example:
In a windows 10.0 system:
import sys
print(sys.getwindowsversion())
Output:
sys.getwindowsversion(major=10, minor=0, build=22000, platform=2, service_pack='')

Now that we covered the basic built-in functions and attributes in the sys module, we'll learn about the standard streams:

First, what is a stream?

To execute a program:

  1. The program can receive data from different sources
  2. We can send the program's data to several destinations.

The data source, program, and destination interactions are called streams.

Python interacts with the input, output devices via the standard input, and output stream objects in the sys module called stdin and stdout:

stdin

stdin-standard input.

It is a predefined input stream object that handles the data flow into the program from sources. For example, the user gives input via the keyboard. When we write input(), the function reads the bytes from a standard input stream device, usually a keyboard. The built-in function input() abstracts all of the mechanisms. It is the wrapper function of sys.stdin.readline(). We can even redirect the input source from the keyboard to another device or a file.

Here is a simple example for taking input using stdin:

1. Open notepad or another text editor.

2. Enter the code snippet and save it as a python file(.py)

You can observe that in the above snippet, sys.stdin acts like a file, and using readlines(), we are reading it.

3. Now, open command prompt:

Change the directory to the file location using the command:


4. Enter the input and kill it using Ctrl+ z. You will be able to see all the inputs entered along with the length.

Functions and file objects in Python sys module

5. We took input from the user directly from the command line.

sys.stdin.readline() and input():

We can use both functions to take input in Python. There are some notable differences between their functionalities:

  1. readlines() reads the new line escape character at the end while input() does not.
  2. readline() has a parameter called size that takes the number of characters it can read, while input() has a prompt or a string that we can give to print before taking the input.

stdout:

stdout: standard output.

It is a predefined output stream object that handles the data from the program to different destinations. For example, we use print() to display something on the console/ screen. Here, the standard output stream device is the console. It allows us to interact directly with the command line and print something to it.

Here is a simple example:

1. Open notepad or another text editor.

2. Enter the code snippet and save it as a python file(.py)

You can observe that in the above snippet, sys.stdout acts like a file, and using write(), we are writing to it.

3. Now, open command prompt:

Change the directory to the file location using the command:


4. Enter the input and kill it using Ctrl+ z. You will be able to see all the inputs entered along with the length.

Functions and file objects in Python sys module

5. As you can observe, the function sys.stdout.write() doesn't, by default, add a new line escape character at the end. We need to add it.

stderr:

stderr: Standard error stream.

Like the stdout stream, stderr also is an output stream. The difference between stdout and stderr is that stderr is used to print or output errors, exceptions, and debug information conventionally. The standard error stream device is the console. We can also use stdout to output error information, but stderr is specially designed for this task.

Here is a simple example:

1. Open notepad or another text editor.

2. Enter the code snippet and save it as a python file(.py)

You can observe that in the above snippet, sys.stderr acts like a file, and using write(), we are writing to it.

3. Now, open command prompt:

Change the directory to the file location using the command:


4. Enter the input and kill it using Ctrl+ z. You will be able to see all the inputs entered along with the length.

Functions and file objects in Python sys module

5. This function also writes to the IDE console, unlike stdout.write():

Functions and file objects in Python sys module

Redirection:

Generally, the standard input stream device is a keyboard, and the standard output stream device is the system console. We can change these devices according to our needs by

1. Shell redirection.

  • < Operator refers to input. When we say < file_name, input is taken from the specified file.
  • > Operator refers to output. When we say, > file name, the program's output is given to the specified file.

Here is an example:

  1. Open notepad or another text editor.
  2. Enter the code snippet and save it as a python file(.py)

3. Now, open command prompt:

Change the directory to the file location using the command:

Use the command:

to redirect the output of source.py to the destination file.

Functions and file objects in Python sys module
Functions and file objects in Python sys module

2. sing stdout:

1D.txt:

Functions and file objects in Python sys module




Latest Courses