python3通过subprocess模块调用脚本并和脚本交互的操

1. Introduction

In this article, we will explore how to use the subprocess module in Python 3 to call and interact with scripts. The subprocess module allows us to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

2. Calling a Script using subprocess

2.1 Spawning a Process

To call a script using subprocess, we first need to import the module:

import subprocess

Next, we can use the subprocess.run() function to call our script. The function takes a list of arguments, where the first argument is the script name or path and the following arguments are any command-line arguments the script requires:

subprocess.run(['python', 'script.py', arg1, arg2])

Here, we are calling the script.py script with two command-line arguments arg1 and arg2.

2.2 Running a Script in the Background

If we want to run the script in the background without waiting for it to complete, we can use the subprocess.Popen() function:

subprocess.Popen(['python', 'script.py'])

This will start the script and return immediately, allowing the parent process to continue executing.

3. Interacting with a Script

3.1 Capturing Script Output

We can capture the output of a script by setting the subprocess.PIPE flag for the stdout or stderr arguments of the subprocess.run() or subprocess.Popen() functions:

result = subprocess.run(['python', 'script.py'], stdout=subprocess.PIPE)

output = result.stdout

In this example, we are capturing the standard output of the script by setting stdout=subprocess.PIPE. The resulting output can then be accessed through the stdout attribute of the result object.

3.2 Passing Input to a Script

We can pass input to a script by setting the input argument of the subprocess.run() function. The input can be a string or bytes-like object:

result = subprocess.run(['python', 'script.py'], input='hello', text=True, capture_output=True)

output = result.stdout

Here, we are passing the string 'hello' as input to the script. We also set the text flag to indicate that the input and output should be treated as text.

4. Advanced Techniques

4.1 Setting the Working Directory

By default, the script is called from the current working directory of the parent process. We can change the working directory by setting the cwd argument:

subprocess.run(['python', 'script.py'], cwd='/path/to/directory')

This will start the script in the specified directory.

4.2 Setting the Environment Variables

We can set environment variables for the script by passing a dictionary to the env argument of the subprocess.run() function:

env = {'ENV_VAR': 'value'}

subprocess.run(['python', 'script.py'], env=env)

In this example, we set the environment variable ENV_VAR to value before running the script.

5. Conclusion

In this article, we have explored how to call and interact with scripts using the subprocess module in Python 3. We learned how to spawn a process, capture script output, pass input to a script, and use advanced techniques such as setting the working directory and environment variables. The subprocess module provides a powerful interface for interacting with external scripts and processes, making it a valuable tool for various applications.

免责声明:本文来自互联网,本站所有信息(包括但不限于文字、视频、音频、数据及图表),不保证该信息的准确性、真实性、完整性、有效性、及时性、原创性等,版权归属于原作者,如无意侵犯媒体或个人知识产权,请来电或致函告之,本站将在第一时间处理。猿码集站发布此文目的在于促进信息交流,此文观点与本站立场无关,不承担任何责任。

后端开发标签