Let’s use the subprocess library to write a script that pings multiple servers to see whether they are reachable or not. This documentation explores the motivation behind creating this module. Therefore, we are going to use subprocess in this tutorial. This cannot be done in the os module.Īlthough you can execute commands using the OS module, the subprocess library provides a better and newer approach and is officially recommended. The subprocess module returns an object that can be used to get more information on the output of the command and kill or terminate the command if necessary. The os.system function simply runs the shell command and only returns the status code of that command. The subprocess module overcomes these vulnerabilities and is more secure. Os.system directly executes shell commands and is susceptible to vulnerabilities. The subprocess module serves as a replacement to this and Python officially recommends using subprocess for shell commands. In other words, this function has been replaced. The subprocess library provides a better, safer, and faster approach for this and allows us to view and parse the output of the commands. In some commands, it is imperative to read the output and analyze it. However, we can’t read and parse the output of the command. The program above lists all the files inside a directory. The os.system() function allows users to execute commands in Python.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |