How to Control a Unix Shell with Python
The Python programming language supports web and desktop development through its library of built-in modules and functions. Through these modules, you can access system resources such as the command line, and send commands to the operating system. Because of this, you can issue typical Unix shell commands to a Unix operating system and use the results in your Python program.
Instructions
-
-
1
Import the "os" module:
>>>import os
-
2
Issue a command to the shell through the "popen" function, and save the returned results in a variable. The popen function sends the command to a command shell, which executes the command in the operating system:
>>>l = os.popen('ls'')
-
-
3
Read the commands from the variable using the "readline" function. This will return the first line of output from the shell command:
>>>l.readline()
'/etc'
-
1