Use the Python 'Sarge' library to execute the external program in the command line
Using Python's 'Sarge' library can easily perform external programs in the command line.The library provides a simple way to manage the execution of external commands and can easily capture and process its output.
The following is an example program that demonstrates how to use the 'Sarge' library to perform external programs:
First, we need to install the 'Sarge' library.You can use the following command to install it in the terminal:
pip install sarge
After the installation is completed, we can start writing the program.The following is a simple example that demonstrates how to run external programs and obtain its output:
python
from sarge import run, Capture
# Define the external command to be executed
command = "ls"
# Use the RUN method to execute the external command and capture the output
output = run(command, stdout=Capture())
# Print the output of external commands
print(output.stdout.text)
In the above example, we first introduced the Run and Capture class in the 'Sarge' library.Next, we define the external commands to be executed. This is "ls", that is, the file list of the current directory.
We then use the Run method to execute external commands and capture it into the Capture object.Here, we designate the Stdout parameter to capture the standard output.
Finally, we obtain the output of external commands by accessing the Stdout property of the Output object and print it out.
This is a simple example that shows how to use the 'Sarge' library to run external programs and obtain its output.According to actual needs, you can use more 'sarge' functions and parameters to process the execution of external commands.