Command Line Automation in Python


Python comes with a command line for managing user input and specific forms of data entering while Python applications are being executed. As a result, users can enter data and complete tasks otherwise that would be impossible. This also enables more complex tasks and increased program interaction.

To communicate with computers and execute programs, one uses a command-line interface (CLI), and a text-based user interface (UI). Additional names for command-line interfaces include character user interfaces, console user interfaces, and command-line user interfaces.

In this article, you will learn what the Python command-line interface (CLI) is and how to automate the command line in python (A framework is triggered throughout the process of automating a task. This framework, which serves as our programming script, enables the activity to be completed autonomously, independently, and without user participation.). Furthermore, you will also see some code samples implementing them.

Introduction to Python

Python is a widely used high-level, general-purpose, object-oriented, and interpreted language. To utilize Python for a task, one only needs to supply the logic because Python itself handles the majority of the coding work. Python has a highly comprehensive library containing pre-defined code for every use.

Python has a sizable developer community, which gives newbies and seasoned users an added advantage in that there are never any problems.

Introduction to Command Line Interface

An approach to interacting with a computer program in which the user (or client) issues commands to the program in the form of successive lines of text (command lines) is known as a command-line interface or command language interpreter (CLI), also known as command-line user interface, console user interface, and character user interface (CUI).

A user can communicate with a program in a text-based shell interpreter via a command line interface (CLI). Shell interpreters include programs like Bash on Linux or Command Prompt on Windows. The shell interpreter exposes a command prompt and makes a command line interface available.

To run Python scripts with the python command we have to a process which is described below −

The command "python" can be used to start Python scripts. To do so, open a command window and type "python" (or "python3" if you have both versions installed) followed by the script's path

Example

python3 hello.py Hello World, Welcome!

Hello World, Welcome! If everything goes as expected, the message "Hello World, Welcome" will appear once when you press Enter.

Command Line automation in Python

The CLI makes it possible to write scripts for basic automation, and it is a helpful tool for writing more intricate code that uses the web service API.

Python comes with a command line for managing user input and specific forms of data entering while Python applications are being executed. As a result, users can enter data and complete tasks that would be otherwise impossible. This also enables more complex work and increased program interaction.

How to automate the command line in python?

There are various commands that can be given in the command line so a purpose can be fulfilled on a computer or PC like opening any app or shutting down the computer. The commands we can provide can be found using the ‘help’ command on the command line. Using the help command we can get all the commands we can write on the command line.

In this article, we are going to use the ‘os’ module of python to work with the command line using the python programming language. Let’s see the code of this here

Example

import os os.system('cmd ')

By running the above code we can open the command prompt in the pc using python. system() is the method to call the application we want to open with the operating system.

Now, this is the way by which we can open the command prompt, but to work automatically with the command prompt we have to add some more commands as the parameters of the system() method.

There are two ways to perform a task using the command line automatically, we can either add the ‘/k’ or ‘/c’ with the cmd and then write the command to perform.

  • Method 1 with ‘/k’

    import os os.system('cmd /k')
  • Method 2 with ‘/c’

    import os os.system('cmd /c')

Let's discuss first using the ‘/k’

Using the ‘/k’ command is not advisable because it will not close the prompt after the use which may create some security breaches if the data is sensitive or in the live project.

Using the ‘/c’ command is safe as after the compilation it will close the process and return zero after the compilation.

Note − In this article, we are going to use the ‘/c’ method only.

Now, let’s move to the complete code to run any command

Example

import os os.system('cmd /c "command" ')

Here the command is the command which we want to run and it will be wrapped up in the double quotes, for example

Example

import os os.system('cmd /c "date" ')

After running this code we will get the data on the command line automatically.

Example 

import os os.system('cmd /c "start chrome" ')

After running this command the command line prompt will open if the command is not given from the command line prompt itself and after that, the chrome will open.

To make things automatic we can use the python library time and give some delay’s so that things work after some time.

Also to give two or more commands we can the ‘and’ operator of the python programming language and the commands will run in the manner they are written in the double quotes. For example

Example

import os
os.system('cmd /c "date && start chrome" ')

In the above code, first, the date will come on the screen then the chrome window will open.

An example of time delay can be seen using the time library to provide some delay in the tasks

Example

import time import os for i in range(5): os.system('cmd /c "date"') time.sleep(5)

The above code will show the date in the command line after every five second as we have sued the sleep method of the time library.

Conclusion

Python comes with a command line for managing user input and specific forms of data entering while Python applications are being executed. To communicate with computers and execute programs, one uses a command-line interface (CLI), and a text-based user interface (UI). We have used the ‘os’ module of python to work with the command line using the python programming language. Using the ‘/c’ command is safe as after the compilation it will close the process and return zero after the compilation. Also to give two or more commands we can the ‘and’ operator of the python programming language and the commands will run in the manner they are written in the double quotes.

Updated on: 11-Jan-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements