Home > Backend Development > Python Tutorial > How to run Python script in Linux system and get the results

How to run Python script in Linux system and get the results

PHPz
Release: 2023-10-05 08:57:31
Original
1224 people have browsed it

How to run Python script in Linux system and get the results

How to run a Python script in a Linux system and get the results

When using a Linux system to develop and run Python scripts, we often need to run a script and get its results operation result. This article will introduce how to run a Python script in a Linux system and obtain its output. Below are the specific steps and sample code.

  1. Create a Python script
    First, we need to create a Python script as an example. Suppose we want to create a simple script that calculates the sum of two numbers. Create a file called "sum.py" in the terminal using any text editor and enter the following code:
# sum.py

def sum_numbers(a, b):
    return a + b

result = sum_numbers(5, 3)
print("The sum is:", result)
Copy after login

Save and close the file.

  1. Add executable permissions to the script
    Use the following command in the terminal to add executable permissions to the script:
chmod +x sum.py
Copy after login
  1. Run the script and get the results
    Use the following command to run the script in the terminal and get its output:
./sum.py
Copy after login

You will see the following output:

The sum is: 8
Copy after login

In this way, you will successfully Run the Python script on the Linux system and obtain the results.

  1. Get the script output result
    If you want to save the script output result to a variable for further processing, you can use the subprocess module subprocess to achieve this. The following is a sample code:
# execute_script.py

import subprocess

script_output = subprocess.check_output(["./sum.py"]).decode("utf-8")
print("Script output:", script_output)
Copy after login

In this example, first import the subprocess module, and then use the check_output function to run the script and get its output. Note that we use the decode function to convert the output from a byte stream to a string and print it out.

Save and close the file.

  1. Run the execution script and get the results
    Use the following command to run the execution script in the terminal and get its output results:
python execute_script.py
Copy after login

You will see the following Output result:

Script output: The sum is: 8
Copy after login

In this way, you have successfully run the Python script in the Linux system and obtained its output through the child process.

Summary:
In a Linux system, we can run a Python script and obtain its output in simple steps. First, we need to add executable permissions to the script. You can then run the script directly in the terminal and view the output. If you want to save the output results to a variable, you can use the check_output function of the subprocess module subprocess to accomplish this. I hope this article can help you use Python scripts to run and obtain results in Linux systems.

The above is the detailed content of How to run Python script in Linux system and get the results. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template