CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Python : Read Input – Console and File based Input

Would like to write Articles, on Input & Output processing in Python. Through this article, I would like to explain how to read Input using Python. There are several functions in Python that handle I/O processing.

Before we start, I would like to give a quick introduction to the build-in function, pint(); which is one of the widely used functions used to write the output on the console window. Would like to explain more about the “print()” built-in function in upcoming Articles.

Usually when we write the program in any language; we consider, giving input to the program; so that the program will behave/work differently depending on the input received. The usual way of giving the input is; the user will give the input by typing something on the console window, or reading file contents, or generating some events, either by other Programs or the Operating System, etc.

We will discuss in this article, console-based input & file-based input.

Console based Input

To get the user input, generally, we use the built-in function input().

Note that, in older versions of Python, we have raw_input() function for getting the user input from the console. From Python 3.x, raw_input() function was replaced with the built-in function input().

when the Python interpreter, executes the input() statement; it will halt for the user’s input. Once the user has given the input & press Enter key; the entered value will be considered. For example, the below statements read the input from the user & print the given input on the console.

>>> n = input()
10
>>> print (n)
10
>>> n = input()
'hello'
>>> print (n)
'hello'
>>> 

Always input() function, read the input as text. That means, if you enter 10, it read it as text “10”. If you apply any numerical operations on the entered value, Python will through the error. In order to use them in numerical computation, we must convert the text value into integer value before we use the value in the computation. For example, the below code shows how Python throws the Error and how the program handled it by converting the text to an integer by using int() built-in function.

>>> n = input()
10
>>> print (n+20)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: must be str, not int
>>> 
>>> 
>>> print (int(n)+20)
30
>>> 

File-based Input

There are some built-in functions; deal with file-based input & output operations. The best example for file-based input is, Program’s configuration file; the program will behave depending on the settings or configuration mentioned in the configuration file. This way, we no need to modify the program every time there is a change in the configuration; instead, we modify the configuration file & the program will show the result depending on the configuration settings. For example, if you would like to display your program’s window in a different color; based on the color value mentioned in the configuration file; you do need to modify your program whenever you want to change the color of the window; instead, modify the configuration file will display the program’s window in a different color.

For file-based input, usually below steps, we should follow:

  • open() the file.
  • Do the file operation(s). Once done with the file,
  • close() the file.

For file-based operations, Python provides; many functions, but through this article, we will go through a few of them, briefly. I may write, more details of these functions; through separate Articles.

  • open() function will open the file for reading “r” or for write “w” or for append “a”. To open for both read & write we can use “r+” or “w+” and “a+”.
  • readline() function is useful to read a line of text from the file.
  • close() function is to close the file. This must be called, once done with all the file operations.

I will give one simple example here. Lets’ display the colored text on the screen; by getting the color values from a simple configuration file.

  • I will create a file “color.cnf“, and place a color value in it. One for background color & another for text color. I referred to the Article “Computer Concepts : Display Colorful text on the Screen” for color values & how to apply them.
  • Through the Python program, read the “color.cnf” file; and apply the color to the displayed text.
  • Modify the “color.cnf” file with new color values.
  • Again run the program, and observe the results.

For this, we need to use, “open()“, “readline()” & “close()” ; file-related functions.



Create the file “color.cnf” with the below values:

47
35

Lets’ write below program “readinput.py” to read the values from “color.cnf” and apply them to change the text attributes.

# readinput.py
file = open("color.cnf", "r")
if ( file != None ):
    bkgrnd = file.readline()
    txtcolor = file.readline()
    file.close()

    print('\x1b[' + bkgrnd + ';' + txtcolor + 'm')
    print('Hello, World!')

    print('\x1b[0m')

After running it, Magenta color text will be displayed on White background.

Now change the “conf.cnf” file with the below values:

45
37

Again, run our program and observe that; now White color text is displayed on Magenta’s background.

We have other types of Inputs to the programs; I may cover them through separate Articles.

Feel free to write your feedback below Comments.

/Shijit/

Python : Read Input – Console and File based Input

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top