Functions and file objects in Python sys module17 Mar 2025 | 5 min read 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:
Basic functions and variables in the sys module:
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:
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: stdinstdin-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. ![]() 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:
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. ![]() 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. ![]() 5. This function also writes to the IDE console, unlike stdout.write(): ![]() 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.
Here is an example:
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. ![]() ![]() 2. sing stdout: 1D.txt: ![]() Next TopicWhat is a Binary Heap in Python |
Mutable and Immutable Data Types in Python Mutable or immutable is the fancy word for explaining the property of data types of being able to get updated after being initialized. The basic explanation is thus: A mutable object is one whose internal state is changeable. On the...
7 min read
In this tutorial, we will learn how to verify a list consists of duplicate elements or not. It is a basic list program that can be asked in the coding interview. We will solve this problem using various methods. Let's see the problem statement. Problem Statement An integer...
4 min read
This article will teach us how to use Python to track Amazon items we want to buy. We often purchase the item provided it goes under a particular limit to keep it affordable and expand our reserve funds. In this venture, we will precisely carry out...
4 min read
Introduction: Cryptography is the art of writing codes or ciphers to secure communications between two parties. One of the most popular ciphers is the Hill cipher, which is a polygraphicsubstitution cipher. Unlike a monoalphabetic cipher, which substitutes one letter for another, the Hill cipher uses matrices to...
6 min read
We have worked with different kind of numbers in Python and modified their type as per our need. In this tutorial, we will discuss how we can remove decimal in Python. Let's start with a simple program, a = 24 print(type(a)) b = 19.4 print(type(b)) c = 3+4j print(type(c)) Output: <class 'int'> <class 'float'> <class 'complex'> Explanation: In the above...
2 min read
This tutorial will teach us how to concatenate tuples into nested ones. Sometimes, we need to convert the individual records into a nested collection yet found as a separate element. In other words, we will add the tuples and flattens the resultant container; it is usually...
3 min read
There are many times when we are working on someone else's system, and we have to complete our project there. It becomes more hectic when we have to install all the required modules in that system. It is actually messier when we have to start from...
4 min read
? Using Python's tolist() function, you may turn an array into a list. Here is an example: import array arr = array.array("i", [1, 2, 3, 4, 5]) lst = arr.tolist() print(type(lst)) # <class 'list'> print(lst) Output: [1, 2, 3, 4, 5] In this example, the array.array() constructor is used to create an array arr...
2 min read
Introduction In computer science, the Disjoint Set, commonly called the Union-Find data structure, is an effective tool for maintaining sets of objects and providing answers to queries regarding their connectedness. The Union-Find Algorithm in Python, frequently used to create the Disjoint Set, is incredibly effective at solving...
4 min read
In this tutorial, we will learn how to display the calendar of any month of any year using Python. In the code below, we will import the "calendar" module. It has an inbuilt "month()" function, which takes the year and month of which the user wants to...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India