How to use sys and time In Python

Prerequisites:

  • Sys:

Functions and variables for modifying many elements of the Python runtime environment are available in the sys module.

The following code can be used to import this built-in module without having to install it first:

import sys
  • Time:

Accessing time in Python is made possible by the time module. It has capabilities like the ability to delay the execution of the application and the ability to get the current date and time. This module needs to be imported before we can start using its features.

This is an in-built module and need not be installed, it can be imported directly using the code:

import time
  • Random:

Python comes with a built-in module called Python Random that may be used to create random integers. Since they are not completely random, these numbers are pseudo-random. This module may be utilized to carry out random operations like generating random integers, printing a random value for a list or string, etc.

It is not necessary to install this built-in module because it may be imported directly using the following code:

import random

Code:

Python3




import sys,time,random
def progressBar(count_value, total, suffix=''):
    bar_length = 100
    filled_up_Length = int(round(bar_length* count_value / float(total)))
    percentage = round(100.0 * count_value/float(total),1)
    bar = '=' * filled_up_Length + '-' * (bar_length - filled_up_Length)
    sys.stdout.write('[%s] %s%s ...%s\r' %(bar, percentage, '%', suffix))
    sys.stdout.flush()
for i in range(11):
    time.sleep(random.random())
    progressBar(i,10)
#This code is Contributed by PL VISHNUPPRIYAN


Output:

Output in Terminal Window



Progress Bars in Python

Understandably, we get a little impatient when we do not know how much time a process is going to take, for example, a for loop or a file downloading or an application starting up. To distract us from that we were given the libraries tqdm and progressbar in Python language which allows us to give a visual illustration of the process completion time using a progress bar. Loading bars are often seen on game screens as the resources required for the game to run are being acquired to the main memory.

Similar Reads

Using tqdm

What It Does...

Using progressbar

...

Using sys and time

...