Quy trình con python w3schools

Mô-đun

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2 cho phép bạn sinh ra các quy trình mới, kết nối với các đường ống đầu vào/đầu ra/lỗi của chúng và lấy mã trả về của chúng. Mô-đun này dự định thay thế một số mô-đun và chức năng cũ hơn

Show

os.system
os.spawn*

Thông tin về cách mô-đun

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2 có thể được sử dụng để thay thế các mô-đun và chức năng này có thể được tìm thấy trong các phần sau

Xem thêm

PEP 324 – PEP đề xuất mô-đun quy trình con

Tính khả dụng . không phải Emscripten, không phải WASI.

Mô-đun này không hoạt động hoặc không khả dụng trên nền tảng WebAssembly

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
5 và
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
6. Xem Nền tảng WebAssugging để biết thêm thông tin.

Using the >>> import shlex, subprocess >>> command_line = input() /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'" >>> args = shlex.split(command_line) >>> print(args) ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"] >>> p = subprocess.Popen(args) # Success! 2 Module¶

The recommended approach to invoking subprocesses is to use the

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 function for all use cases it can handle. For more advanced use cases, the underlying
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 interface can be used directly

subprocess. chạy(args , *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs)

Run the command described by args. Wait for command to complete, then return a

Popen(['/bin/sh', '-c', args[0], args[1], ...])
0 instance

The arguments shown above are merely the most common ones, described below in Frequently Used Arguments (hence the use of keyword-only notation in the abbreviated signature). The full function signature is largely the same as that of the

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 constructor - most of the arguments to this function are passed through to that interface. (timeout, input, check, and capture_output are not. )

If capture_output is true, stdout and stderr will be captured. When used, the internal

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 object is automatically created with
Popen(['/bin/sh', '-c', args[0], args[1], ...])
3 and
Popen(['/bin/sh', '-c', args[0], args[1], ...])
4. The stdout and stderr arguments may not be supplied at the same time as capture_output. If you wish to capture and combine both streams into one, use
Popen(['/bin/sh', '-c', args[0], args[1], ...])
3 and
Popen(['/bin/sh', '-c', args[0], args[1], ...])
6 instead of capture_output

The timeout argument is passed to

Popen(['/bin/sh', '-c', args[0], args[1], ...])
7. If the timeout expires, the child process will be killed and waited for. The
Popen(['/bin/sh', '-c', args[0], args[1], ...])
8 exception will be re-raised after the child process has terminated

The input argument is passed to

Popen(['/bin/sh', '-c', args[0], args[1], ...])
7 and thus to the subprocess’s stdin. If used it must be a byte sequence, or a string if encoding or errors is specified or text is true. When used, the internal
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 object is automatically created with
os.system
os.spawn*
91, and the stdin argument may not be used as well

If check is true, and the process exits with a non-zero exit code, a

os.system
os.spawn*
92 exception will be raised. Attributes of that exception hold the arguments, the exit code, and stdout and stderr if they were captured

If encoding or errors are specified, or text is true, file objects for stdin, stdout and stderr are opened in text mode using the specified encoding and errors or the

os.system
os.spawn*
93 default. The universal_newlines argument is equivalent to text and is provided for backwards compatibility. By default, file objects are opened in binary mode

If env is not

os.system
os.spawn*
94, it must be a mapping that defines the environment variables for the new process; these are used instead of the default behavior of inheriting the current process’ environment. It is passed directly to
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9. This mapping can be str to str on any platform or bytes to bytes on POSIX platforms much like
os.system
os.spawn*
96 or
os.system
os.spawn*
97

Examples

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
6

New in version 3. 5

Changed in version 3. 6. Added encoding and errors parameters

Changed in version 3. 7. Added the text parameter, as a more understandable alias of universal_newlines. Added the capture_output parameter.

class subprocess. CompletedProcess

The return value from

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8, representing a process that has finished

args

The arguments used to launch the process. This may be a list or a string

returncode

Exit status of the child process. Typically, an exit status of 0 indicates that it ran successfully

A negative value

os.system
os.spawn*
99 indicates that the child was terminated by signal
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
90 (POSIX only)

stdout

Thiết bị xuất chuẩn bị bắt từ tiến trình con. A bytes sequence, or a string if

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 was called with an encoding, errors, or text=True.
os.system
os.spawn*
94 if stdout was not captured

If you ran the process with

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
93, stdout and stderr will be combined in this attribute, and
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
94 will be
os.system
os.spawn*
94

stderr

Captured stderr from the child process. A bytes sequence, or a string if

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 was called with an encoding, errors, or text=True.
os.system
os.spawn*
94 if stderr was not captured

check_returncode()

If

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
98 is non-zero, raise a
os.system
os.spawn*
92

New in version 3. 5

subprocess. DEVNULL

Special value that can be used as the stdin, stdout or stderr argument to

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 and indicates that the special file
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
01 will be used

New in version 3. 3

subprocess. PIPE

Special value that can be used as the stdin, stdout or stderr argument to

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 and indicates that a pipe to the standard stream should be opened. Most useful with
Popen(['/bin/sh', '-c', args[0], args[1], ...])
7

subprocess. STDOUT

Special value that can be used as the stderr argument to

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 and indicates that standard error should go into the same handle as standard output

exception subprocess. SubprocessError

Base class for all other exceptions from this module

New in version 3. 3

exception subprocess. TimeoutExpired

Subclass of

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
05, raised when a timeout expires while waiting for a child process

cmd

Command that was used to spawn the child process

timeout

Timeout in seconds

output

Output of the child process if it was captured by

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 or
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
07. Otherwise,
os.system
os.spawn*
94. This is always
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
09 when any output was captured regardless of the
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
90 setting. It may remain
os.system
os.spawn*
94 instead of
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
92 when no output was observed

stdout

Alias for output, for symmetry with

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
94

stderr

Stderr output of the child process if it was captured by

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8. Otherwise,
os.system
os.spawn*
94. This is always
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
09 when stderr output was captured regardless of the
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
90 setting. Nó có thể vẫn là
os.system
os.spawn*
94 thay vì
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
92 khi không quan sát thấy đầu ra stderr

New in version 3. 3

Changed in version 3. 5. đã thêm các thuộc tính stdout và stderr

exception subprocess. CalledProcessError

Lớp con của

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
05, được nâng lên khi một quy trình được chạy bởi
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
01,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
07 hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 (với
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
04) trả về trạng thái thoát khác không

returncode

Trạng thái thoát của tiến trình con. Nếu quá trình thoát do tín hiệu, đây sẽ là số tín hiệu âm

cmd

Command that was used to spawn the child process

output

Đầu ra của tiến trình con nếu nó được chụp bởi

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
07. Mặt khác,
os.system
os.spawn*
94

stdout

Alias for output, for symmetry with

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
94

stderr

Đầu ra Stderr của tiến trình con nếu nó bị bắt giữ bởi

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8. Mặt khác,
os.system
os.spawn*
94

Changed in version 3. 5. đã thêm các thuộc tính stdout và stderr

Đối số thường được sử dụng¶

Để hỗ trợ nhiều trường hợp sử dụng khác nhau, hàm tạo

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 (và các hàm tiện lợi) chấp nhận một số lượng lớn các đối số tùy chọn. Đối với hầu hết các trường hợp sử dụng điển hình, nhiều đối số trong số này có thể được để nguyên một cách an toàn ở các giá trị mặc định của chúng. Các đối số thường cần thiết nhất là

args là bắt buộc đối với tất cả các cuộc gọi và phải là một chuỗi hoặc một chuỗi các đối số của chương trình. Việc cung cấp một chuỗi các đối số thường được ưu tiên hơn, vì nó cho phép mô-đun xử lý mọi yêu cầu thoát và trích dẫn các đối số (e. g. để cho phép khoảng trắng trong tên tệp). If passing a single string, either shell must be

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
602 (see below) or else the string must simply name the program to be executed without specifying any arguments

stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
603,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
604, an existing file descriptor (a positive integer), an existing file object with a valid file descriptor, and
os.system
os.spawn*
94.
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
603 indicates that a new pipe to the child should be created.
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
604 indicates that the special file
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
01 will be used. With the default settings of
os.system
os.spawn*
94, no redirection will occur; the child’s file handles will be inherited from the parent. Additionally, stderr can be
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
610, which indicates that the stderr data from the child process should be captured into the same file handle as for stdout

If encoding or errors are specified, or text (also known as universal_newlines) is true, the file objects stdin, stdout and stderr will be opened in text mode using the encoding and errors specified in the call or the defaults for

os.system
os.spawn*
93

For stdin, line ending characters

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
612 in the input will be converted to the default line separator
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
613. For stdout and stderr, all line endings in the output will be converted to
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
612. For more information see the documentation of the
os.system
os.spawn*
93 class when the newline argument to its constructor is
os.system
os.spawn*
94

If text mode is not used, stdin, stdout and stderr will be opened as binary streams. No encoding or line ending conversion is performed

New in version 3. 6. Added encoding and errors parameters.

New in version 3. 7. Added the text parameter as an alias for universal_newlines.

Note

The newlines attribute of the file objects

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
617,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
618 and
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
619 are not updated by the
Popen(['/bin/sh', '-c', args[0], args[1], ...])
7 method

If shell is

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
602, the specified command will be executed through the shell. This can be useful if you are using Python primarily for the enhanced control flow it offers over most system shells and still want convenient access to other shell features such as shell pipes, filename wildcards, environment variable expansion, and expansion of
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
622 to a user’s home directory. However, note that Python itself offers implementations of many shell-like features (in particular,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
623,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
624,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
625,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
626,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
627, and
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
628)

Changed in version 3. 3. When universal_newlines is

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
602, the class uses the encoding
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
630 instead of
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
631. See the
os.system
os.spawn*
93 class for more information on this change.

Note

Read the Security Considerations section before using

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
633

These options, along with all of the other options, are described in more detail in the

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 constructor documentation

Popen Constructor¶

The underlying process creation and management in this module is handled by the

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 class. It offers a lot of flexibility so that developers are able to handle the less common cases not covered by the convenience functions

class subprocess. Popen(args , bufsize=- 1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, group=None, extra_groups=None, user=None, umask=- 1, encoding=None, errors=None, text=None, pipesize=- 1, process_group=None)

Execute a child program in a new process. On POSIX, the class uses

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
636-like behavior to execute the child program. On Windows, the class uses the Windows
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
637 function. The arguments to
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 are as follows

args should be a sequence of program arguments or else a single string or path-like object . By default, the program to execute is the first item in args if args is a sequence. Nếu args là một chuỗi, thì việc giải thích phụ thuộc vào nền tảng và được mô tả bên dưới. Xem trình bao và các đối số thực thi để biết thêm sự khác biệt so với hành vi mặc định. Trừ khi có quy định khác, bạn nên chuyển args dưới dạng chuỗi.

Cảnh báo

Để có độ tin cậy tối đa, hãy sử dụng đường dẫn đủ điều kiện cho tệp thực thi. Để tìm kiếm một tên không đủ tiêu chuẩn trên

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
639, hãy sử dụng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
640. Trên tất cả các nền tảng, nên sử dụng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
641 để khởi chạy lại trình thông dịch Python hiện tại và sử dụng định dạng dòng lệnh
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
642 để khởi chạy một mô-đun đã cài đặt

Việc giải quyết đường dẫn của tệp thực thi (hoặc mục đầu tiên của đối số) phụ thuộc vào nền tảng. Đối với POSIX, hãy xem

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
636 và lưu ý rằng khi giải quyết hoặc tìm kiếm đường dẫn thực thi, cwd sẽ ghi đè thư mục làm việc hiện tại và env có thể ghi đè biến môi trường
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
639. Đối với Windows, hãy xem tài liệu về tham số
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
645 và
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
646 của WinAPI
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
647 và lưu ý rằng khi giải quyết hoặc tìm kiếm đường dẫn thực thi với
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
648, cwd không ghi đè thư mục làm việc hiện tại và env không thể ghi đè biến môi trường
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
639. Sử dụng một đường dẫn đầy đủ sẽ tránh được tất cả các biến thể này

Một ví dụ về việc chuyển một số đối số cho một chương trình bên ngoài dưới dạng trình tự là

os.system
os.spawn*
19

Trên POSIX, nếu args là một chuỗi, thì chuỗi đó được hiểu là tên hoặc đường dẫn của chương trình sẽ thực thi. Tuy nhiên, điều này chỉ có thể thực hiện được nếu không truyền đối số cho chương trình

Note

Có thể không rõ ràng về cách chia lệnh shell thành một chuỗi các đối số, đặc biệt là trong các trường hợp phức tạp.

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
650 có thể minh họa cách xác định mã thông báo chính xác cho các đối số

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!

Đặc biệt lưu ý rằng các tùy chọn (chẳng hạn như -input) và đối số (chẳng hạn như trứng. txt) được phân tách bằng khoảng trắng trong trình bao sẽ nằm trong các phần tử danh sách riêng biệt, trong khi các đối số cần trích dẫn hoặc thoát dấu gạch chéo ngược khi được sử dụng trong trình bao (chẳng hạn như tên tệp chứa khoảng trắng hoặc lệnh echo được hiển thị ở trên) là các phần tử danh sách đơn lẻ

Trên Windows, nếu đối số là một chuỗi, nó sẽ được chuyển đổi thành chuỗi theo cách được mô tả trong Chuyển đổi chuỗi đối số thành chuỗi trên Windows. This is because the underlying

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
637 operates on strings.

Đã thay đổi trong phiên bản 3. 6. tham số args chấp nhận đối tượng giống đường dẫn nếu shell là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
652 và một chuỗi chứa các đối tượng giống đường dẫn trên POSIX.

Đã thay đổi trong phiên bản 3. 8. tham số args chấp nhận một đối tượng giống đường dẫn nếu shell là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
652 và một chuỗi chứa byte và các đối tượng giống đường dẫn trên .

Đối số shell (mặc định là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
652) chỉ định có sử dụng shell làm chương trình để thực thi hay không. Nếu shell là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
602, bạn nên chuyển các đối số dưới dạng chuỗi thay vì dưới dạng chuỗi

Trên POSIX với

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
633, trình bao mặc định là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
657. Nếu args là một chuỗi, thì chuỗi đó chỉ định lệnh thực thi thông qua trình bao. Điều này có nghĩa là chuỗi phải được định dạng chính xác như khi nhập vào dấu nhắc trình bao. Điều này bao gồm, ví dụ, trích dẫn hoặc dấu gạch chéo ngược tên tệp thoát với dấu cách trong đó. Nếu đối số là một chuỗi, mục đầu tiên chỉ định chuỗi lệnh và bất kỳ mục bổ sung nào sẽ được coi là đối số bổ sung cho chính trình bao. Điều đó có nghĩa là,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 tương đương với

________số 8

Trên Windows có

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
633, biến môi trường
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
660 chỉ định trình bao mặc định. Lần duy nhất bạn cần chỉ định
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
633 trên Windows là khi lệnh bạn muốn thực thi được tích hợp sẵn trong trình bao (e. g. thư mục hoặc bản sao). Bạn không cần
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
633 để chạy tệp thực thi hoặc tệp thực thi dựa trên bảng điều khiển

Note

Read the Security Considerations section before using

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
633

bufsize sẽ được cung cấp làm đối số tương ứng cho hàm

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
664 khi tạo các đối tượng tệp đường ống stdin/stdout/stderr

  • >>> import shlex, subprocess
    >>> command_line = input()
    /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
    >>> args = shlex.split(command_line)
    >>> print(args)
    ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
    >>> p = subprocess.Popen(args) # Success!
    
    665 có nghĩa là không có bộ đệm (đọc và ghi là một cuộc gọi hệ thống và có thể trả về ngắn)

  • >>> import shlex, subprocess
    >>> command_line = input()
    /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
    >>> args = shlex.split(command_line)
    >>> print(args)
    ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
    >>> p = subprocess.Popen(args) # Success!
    
    666 có nghĩa là dòng được đệm (chỉ sử dụng được nếu
    >>> import shlex, subprocess
    >>> command_line = input()
    /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
    >>> args = shlex.split(command_line)
    >>> print(args)
    ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
    >>> p = subprocess.Popen(args) # Success!
    
    667 i. e. , ở chế độ văn bản)

  • bất kỳ giá trị dương nào khác có nghĩa là sử dụng bộ đệm có kích thước xấp xỉ đó

  • bufsize âm (mặc định) có nghĩa là mặc định hệ thống của io. DEFAULT_BUFFER_SIZE sẽ được sử dụng

Đã thay đổi trong phiên bản 3. 3. 1. bufsize hiện mặc định là -1 để bật tính năng lưu vào bộ đệm theo mặc định để phù hợp với hành vi mà hầu hết mã mong đợi. Trong các phiên bản trước Python 3. 2. 4 và 3. 3. 1 nó được mặc định không chính xác thành

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
665, không có bộ đệm và cho phép đọc ngắn. Điều này là không cố ý và không phù hợp với hành vi của Python 2 như hầu hết các mã mong đợi.

Đối số thực thi chỉ định một chương trình thay thế để thực thi. Nó rất hiếm khi cần thiết. Khi

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
648, tệp thực thi sẽ thay thế chương trình để thực thi được chỉ định bởi args. Tuy nhiên, các đối số ban đầu vẫn được chuyển đến chương trình. Hầu hết các chương trình coi chương trình được chỉ định bởi args là tên lệnh, sau đó có thể khác với chương trình thực sự được thực thi. Trên POSIX, tên args trở thành tên hiển thị cho tệp thực thi trong các tiện ích như ps. Nếu
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
633, trên POSIX, đối số có thể thực thi chỉ định trình bao thay thế cho
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
657 mặc định

Đã thay đổi trong phiên bản 3. 6. tham số thực thi chấp nhận đối tượng giống đường dẫn trên POSIX.

Đã thay đổi trong phiên bản 3. 8. tham số thực thi chấp nhận byte và đối tượng giống đường dẫn trên Windows.

stdin, stdout và stderr chỉ định tương ứng đầu vào tiêu chuẩn, đầu ra tiêu chuẩn và xử lý tệp lỗi tiêu chuẩn của chương trình đã thực hiện. Các giá trị hợp lệ là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
603,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
604, một bộ mô tả tệp hiện có (số nguyên dương), một đối tượng tệp hiện có với một bộ mô tả tệp hợp lệ và
os.system
os.spawn*
94.
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
603 chỉ ra rằng nên tạo một đường ống mới cho trẻ.
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
604 chỉ ra rằng tệp đặc biệt
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
01 sẽ được sử dụng. Với cài đặt mặc định là
os.system
os.spawn*
94, sẽ không có chuyển hướng nào xảy ra; . Ngoài ra, thiết bị xuất chuẩn có thể là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
610, điều này cho biết rằng dữ liệu thiết bị xuất chuẩn từ các ứng dụng sẽ được ghi vào cùng một tệp xử lý như đối với thiết bị xuất chuẩn.

Nếu preexec_fn được đặt thành một đối tượng có thể gọi được, thì đối tượng này sẽ được gọi trong tiến trình con ngay trước khi tiến trình con được thực thi. (chỉ POSIX)

Cảnh báo

The preexec_fn parameter is NOT SAFE to use in the presence of threads in your application. Quá trình con có thể bế tắc trước khi exec được gọi

Note

Nếu bạn cần sửa đổi môi trường cho trẻ, hãy sử dụng tham số env thay vì thực hiện trong preexec_fn. Các tham số start_new_session và process_group sẽ thay thế mã sử dụng preexec_fn để gọi

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
680 hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
681 ở phần tử con

Đã thay đổi trong phiên bản 3. 8. Tham số preexec_fn không còn được hỗ trợ trong trình thông dịch phụ. Việc sử dụng tham số trong trình thông dịch phụ làm tăng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
682. Hạn chế mới có thể ảnh hưởng đến các ứng dụng được triển khai trong mod_wsgi, uWSGI và các môi trường nhúng khác.

Nếu close_fds là true, tất cả các bộ mô tả tệp ngoại trừ

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
665,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
666 và
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
685 sẽ bị đóng trước khi tiến trình con được thực thi. Mặt khác, khi close_fds là sai, bộ mô tả tệp tuân theo cờ kế thừa của chúng như được mô tả trong Kế thừa bộ mô tả tệp .

Trên Windows, nếu close_fds là true thì quy trình con sẽ không kế thừa các thẻ điều khiển trừ khi được chuyển rõ ràng trong phần tử

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
686 của
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
687 hoặc bằng cách chuyển hướng thẻ điều khiển tiêu chuẩn

Đã thay đổi trong phiên bản 3. 2. Giá trị mặc định cho close_fds đã được thay đổi từ

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
652 thành giá trị được mô tả ở trên.

Đã thay đổi trong phiên bản 3. 7. Trên Windows, giá trị mặc định cho close_fds đã được thay đổi từ

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
652 thành
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
602 khi chuyển hướng các bộ điều khiển tiêu chuẩn. Giờ đây, có thể đặt close_fds thành
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
602 khi chuyển hướng các tay cầm tiêu chuẩn.

pass_fds là một chuỗi mô tả tệp tùy chọn để tiếp tục mở giữa cha và con. Cung cấp bất kỳ pass_fds nào buộc close_fds phải là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
602. (chỉ POSIX)

Đã thay đổi trong phiên bản 3. 2. Tham số pass_fds đã được thêm vào.

Nếu cwd không phải là

os.system
os.spawn*
94, hàm sẽ thay đổi thư mục làm việc thành cwd trước khi thực hiện lệnh con. cwd có thể là một chuỗi, byte hoặc đối tượng giống đường dẫn . Trên POSIX, hàm tìm kiếm tệp thực thi (hoặc cho mục đầu tiên trong đối số) so với cwd nếu đường dẫn thực thi là đường dẫn tương đối.

Đã thay đổi trong phiên bản 3. 6. Tham số cwd chấp nhận một đối tượng giống đường dẫn trên POSIX.

Đã thay đổi trong phiên bản 3. 7. Tham số cwd chấp nhận một đối tượng giống đường dẫn trên Windows.

Đã thay đổi trong phiên bản 3. 8. Tham số cwd chấp nhận đối tượng byte trên Windows.

Nếu restore_signals là true (mặc định) thì tất cả các tín hiệu mà Python đã đặt thành SIG_IGN sẽ được khôi phục thành SIG_DFL trong quy trình con trước khi thực thi. Hiện tại điều này bao gồm các tín hiệu SIGPIPE, SIGXFZ và SIGXFSZ. (POSIX only)

Changed in version 3. 2. restore_signals đã được thêm.

If start_new_session is true the

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
694 system call will be made in the child process prior to the execution of the subprocess

Tính khả dụng . VỊ TRÍ

Đã thay đổi trong phiên bản 3. 2. start_new_session đã được thêm vào.

Nếu process_group là một số nguyên không âm, lệnh gọi hệ thống

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
695 sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con

Tính khả dụng . VỊ TRÍ

Đã thay đổi trong phiên bản 3. 11. process_group đã được thêm vào.

Nếu nhóm không phải là

os.system
os.spawn*
94, lệnh gọi hệ thống setregid() sẽ được thực hiện trong quy trình con trước khi thực thi quy trình con. Nếu giá trị được cung cấp là một chuỗi, nó sẽ được tra cứu qua
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
697 và giá trị trong
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
698 sẽ được sử dụng. Nếu giá trị là một số nguyên, nó sẽ được truyền nguyên văn. (chỉ POSIX)

Tính khả dụng . VỊ TRÍ

Mới trong phiên bản 3. 9

Nếu extra_groups không phải là

os.system
os.spawn*
94, lệnh gọi hệ thống setgroups() sẽ được thực hiện trong quy trình con trước khi thực thi quy trình con. Các chuỗi được cung cấp trong extra_groups sẽ được tra cứu qua
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
697 và các giá trị trong
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
698 sẽ được sử dụng. Các giá trị số nguyên sẽ được truyền nguyên văn. (chỉ POSIX)

Tính khả dụng . VỊ TRÍ

Mới trong phiên bản 3. 9

Nếu người dùng không phải là

os.system
os.spawn*
94, lệnh gọi hệ thống setreuid() sẽ được thực hiện trong quy trình con trước khi thực thi quy trình con. Nếu giá trị được cung cấp là một chuỗi, nó sẽ được tra cứu qua
os.system
os.spawn*
1903 và giá trị trong
os.system
os.spawn*
1904 sẽ được sử dụng. Nếu giá trị là một số nguyên, nó sẽ được truyền nguyên văn. (chỉ POSIX)

Tính khả dụng . VỊ TRÍ

Mới trong phiên bản 3. 9

Nếu umask không âm, lệnh gọi hệ thống umask() sẽ được thực hiện trong tiến trình con trước khi thực hiện tiến trình con

Tính khả dụng . VỊ TRÍ

Mới trong phiên bản 3. 9

Nếu env không phải là

os.system
os.spawn*
94, thì nó phải là ánh xạ xác định các biến môi trường cho quy trình mới; . Ánh xạ này có thể là str tới str trên bất kỳ nền tảng nào hoặc byte thành byte trên nền tảng POSIX giống như
os.system
os.spawn*
96 hoặc
os.system
os.spawn*
97

Note

Nếu được chỉ định, env phải cung cấp bất kỳ biến nào cần thiết để chương trình thực thi. Trên Windows, để chạy một hợp ngữ song song, env được chỉ định phải bao gồm một

os.system
os.spawn*
1908 hợp lệ

Nếu mã hóa hoặc lỗi được chỉ định hoặc văn bản là đúng, thì các đối tượng tệp stdin, stdout và stderr được mở ở chế độ văn bản với mã hóa và lỗi được chỉ định, như được mô tả ở trên trong . Đối số universal_newlines tương đương với văn bản và được cung cấp để tương thích ngược. Theo mặc định, các đối tượng tệp được mở ở chế độ nhị phân. . The universal_newlines argument is equivalent to text and is provided for backwards compatibility. By default, file objects are opened in binary mode.

Mới trong phiên bản 3. 6. mã hóa và lỗi đã được thêm vào.

Mới trong phiên bản 3. 7. văn bản đã được thêm dưới dạng bí danh dễ đọc hơn cho universal_newlines.

Nếu được cung cấp, startupinfo sẽ là một đối tượng

os.system
os.spawn*
1909, được truyền cho hàm
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
647 bên dưới. các cờ tạo, nếu được cung cấp, có thể là một hoặc nhiều cờ sau

  • os.system
    os.spawn*
    
    1911

  • os.system
    os.spawn*
    
    1912

  • os.system
    os.spawn*
    
    1913

  • os.system
    os.spawn*
    
    1914

  • os.system
    os.spawn*
    
    1915

  • os.system
    os.spawn*
    
    1916

  • os.system
    os.spawn*
    
    1917

  • os.system
    os.spawn*
    
    1918

  • os.system
    os.spawn*
    
    1919

  • os.system
    os.spawn*
    
    1920

  • os.system
    os.spawn*
    
    1921

  • os.system
    os.spawn*
    
    1922

pipesize có thể được sử dụng để thay đổi kích thước của đường ống khi

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
603 được sử dụng cho stdin, stdout hoặc stderr. Kích thước của đường ống chỉ được thay đổi trên các nền tảng hỗ trợ điều này (chỉ Linux tại thời điểm viết bài này). Các nền tảng khác sẽ bỏ qua thông số này

Mới trong phiên bản 3. 10. Tham số

os.system
os.spawn*
1924 đã được thêm vào.

Các đối tượng Popen được hỗ trợ làm trình quản lý bối cảnh thông qua câu lệnh

os.system
os.spawn*
1925. khi thoát, bộ mô tả tệp tiêu chuẩn bị đóng và quá trình được chờ

os.system
os.spawn*
9

Popen và các chức năng khác trong mô-đun này sử dụng nó sẽ tạo ra một sự kiện kiểm tra

os.system
os.spawn*
1926 với các đối số
os.system
os.spawn*
1927,
os.system
os.spawn*
1928,
os.system
os.spawn*
1929 và
os.system
os.spawn*
1930. Giá trị cho
os.system
os.spawn*
1928 có thể là một chuỗi hoặc một danh sách các chuỗi, tùy thuộc vào nền tảng.

Đã thay đổi trong phiên bản 3. 2. Đã thêm hỗ trợ trình quản lý ngữ cảnh.

Đã thay đổi trong phiên bản 3. 6. Popen destructor hiện phát ra cảnh báo

os.system
os.spawn*
1932 nếu tiến trình con vẫn đang chạy.

Đã thay đổi trong phiên bản 3. 8. Popen có thể sử dụng

os.system
os.spawn*
1933 trong một số trường hợp để có hiệu suất tốt hơn. Trên Hệ thống con Windows dành cho Linux và Mô phỏng người dùng QEMU, hàm tạo Popen sử dụng
os.system
os.spawn*
1933 không còn đưa ra ngoại lệ đối với các lỗi như chương trình bị thiếu, nhưng quy trình con không thành công với giá trị khác 0 của
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
98.

Exceptions¶

Các ngoại lệ được đưa ra trong tiến trình con, trước khi chương trình mới bắt đầu thực thi, sẽ được kích hoạt lại trong tiến trình cha

Ngoại lệ phổ biến nhất được đưa ra là

os.system
os.spawn*
1936. Ví dụ, điều này xảy ra khi cố gắng thực thi một tệp không tồn tại. Các ứng dụng nên chuẩn bị cho các trường hợp ngoại lệ
os.system
os.spawn*
1936. Lưu ý rằng, khi
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
633,
os.system
os.spawn*
1936 sẽ chỉ được đứa trẻ nuôi nếu không tìm thấy vỏ đã chọn. Để xác định xem shell không tìm thấy ứng dụng được yêu cầu hay không, cần kiểm tra mã trả về hoặc đầu ra từ quy trình con

Một

os.system
os.spawn*
1940 sẽ được nâng lên nếu
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 được gọi với các đối số không hợp lệ

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
01 và
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
07 sẽ tăng
os.system
os.spawn*
92 nếu quy trình được gọi trả về mã trả về khác không

Tất cả các hàm và phương thức chấp nhận tham số hết thời gian chờ, chẳng hạn như

os.system
os.spawn*
1945 và
Popen(['/bin/sh', '-c', args[0], args[1], ...])
7 sẽ tăng
Popen(['/bin/sh', '-c', args[0], args[1], ...])
8 nếu hết thời gian chờ trước khi quá trình kết thúc

Tất cả các ngoại lệ được xác định trong mô-đun này đều kế thừa từ

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
05

Mới trong phiên bản 3. 3. Lớp cơ sở

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
05 đã được thêm vào.

Cân nhắc về Bảo mật¶

Không giống như một số chức năng popen khác, việc triển khai này sẽ không bao giờ gọi ngầm hệ thống. Điều này có nghĩa là tất cả các ký tự, bao gồm cả siêu ký tự shell, có thể được chuyển an toàn cho các tiến trình con. Nếu trình bao được gọi một cách rõ ràng, thông qua

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
633, thì trách nhiệm của ứng dụng là đảm bảo rằng tất cả các khoảng trắng và ký tự đặc tả được trích dẫn một cách thích hợp để tránh các lỗ hổng chèn trình bao. Trên một số nền tảng , có thể sử dụng
os.system
os.spawn*
1951 để thoát này.

Đồ vật Popen¶

Các thể hiện của lớp

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 có các phương thức sau

Giáo hoàng. thăm dò ý kiến()

Kiểm tra xem tiến trình con đã kết thúc chưa. Đặt và trả về thuộc tính

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
98. Nếu không, trả về
os.system
os.spawn*
94

Giáo hoàng. chờ đã(hết thời gian=Không có)

Đợi quá trình con kết thúc. Đặt và trả về thuộc tính

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
98

Nếu quá trình không kết thúc sau vài giây hết thời gian chờ, hãy đưa ra một ngoại lệ

Popen(['/bin/sh', '-c', args[0], args[1], ...])
8. Bắt ngoại lệ này và thử chờ đợi là an toàn

Note

Điều này sẽ bế tắc khi sử dụng

Popen(['/bin/sh', '-c', args[0], args[1], ...])
3 hoặc
Popen(['/bin/sh', '-c', args[0], args[1], ...])
4 và quy trình con tạo đủ đầu ra cho một đường ống sao cho nó chặn chờ bộ đệm đường ống của hệ điều hành chấp nhận thêm dữ liệu. Sử dụng
Popen(['/bin/sh', '-c', args[0], args[1], ...])
7 khi sử dụng đường ống để tránh điều đó

Note

Chức năng này được thực hiện bằng cách sử dụng vòng lặp bận (cuộc gọi không chặn và giấc ngủ ngắn). Sử dụng mô-đun

os.system
os.spawn*
1960 để chờ không đồng bộ. xem
os.system
os.spawn*
1961

Đã thay đổi trong phiên bản 3. 3. thời gian chờ đã được thêm.

Giáo hoàng. giao tiếp(đầu vào=Không, timeout=None)

Tương tác với quy trình. Gửi dữ liệu đến stdin. Đọc dữ liệu từ thiết bị xuất chuẩn và thiết bị xuất chuẩn, cho đến khi đạt đến cuối tệp. Đợi quá trình kết thúc và đặt thuộc tính

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
98. Đối số đầu vào tùy chọn phải là dữ liệu được gửi đến quy trình con hoặc
os.system
os.spawn*
94, nếu không có dữ liệu nào được gửi đến quy trình con. Nếu các luồng được mở ở chế độ văn bản, đầu vào phải là một chuỗi. Mặt khác, nó phải là byte

os.system
os.spawn*
1964 trả về một tuple
os.system
os.spawn*
1965. Dữ liệu sẽ là chuỗi nếu luồng được mở ở chế độ văn bản;

Lưu ý rằng nếu bạn muốn gửi dữ liệu đến stdin của quy trình, bạn cần tạo đối tượng Popen với

os.system
os.spawn*
91. Tương tự, để lấy bất kỳ thứ gì khác ngoài
os.system
os.spawn*
94 trong bộ dữ liệu kết quả, bạn cũng cần cung cấp thêm
Popen(['/bin/sh', '-c', args[0], args[1], ...])
3 và/hoặc
Popen(['/bin/sh', '-c', args[0], args[1], ...])
4

Nếu quá trình không kết thúc sau khi hết thời gian vài giây, một ngoại lệ

Popen(['/bin/sh', '-c', args[0], args[1], ...])
8 sẽ được đưa ra. Nắm bắt ngoại lệ này và thử lại giao tiếp sẽ không làm mất bất kỳ đầu ra nào

Quá trình con không bị hủy nếu hết thời gian chờ, vì vậy để dọn dẹp đúng cách, một ứng dụng hoạt động tốt nên hủy quá trình con và kết thúc giao tiếp

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9

Note

Dữ liệu đọc được lưu vào bộ nhớ đệm, vì vậy không sử dụng phương pháp này nếu kích thước dữ liệu lớn hoặc không giới hạn

Đã thay đổi trong phiên bản 3. 3. thời gian chờ đã được thêm.

Giáo hoàng. send_signal(tín hiệu)

Gửi tín hiệu tín hiệu cho trẻ

Không làm gì nếu quá trình hoàn thành

Note

Trên Windows, SIGTERM là bí danh của

os.system
os.spawn*
1971. CTRL_C_EVENT và CTRL_BREAK_EVENT có thể được gửi đến các quy trình đã bắt đầu bằng tham số Creationflags bao gồm
os.system
os.spawn*
1912

Giáo hoàng. chấm dứt()

Dừng lại đứa trẻ. Trên hệ điều hành POSIX, phương thức gửi SIGTERM cho con. Trên Windows, chức năng API Win32

os.system
os.spawn*
1973 được gọi để dừng con

Popen. giết()

Giết đứa trẻ. Trên hệ điều hành POSIX, hàm gửi SIGKILL cho con. Trên Windows

os.system
os.spawn*
1974 là bí danh của
os.system
os.spawn*
1971

Các thuộc tính sau đây cũng có sẵn

Giáo hoàng. args

Đối số args khi nó được truyền tới

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 – một chuỗi các đối số chương trình hoặc một chuỗi đơn lẻ

New in version 3. 3

Giáo hoàng. stdin

Nếu đối số stdin là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
603, thì thuộc tính này là đối tượng luồng có thể ghi được trả về bởi
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
664. Nếu đối số mã hóa hoặc lỗi đã được chỉ định hoặc đối số universal_newlines là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
602, thì luồng là luồng văn bản, nếu không thì luồng đó là luồng byte. Nếu đối số stdin không phải là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
603, thì thuộc tính này là
os.system
os.spawn*
94

Giáo hoàng. thiết bị xuất chuẩn

Nếu đối số thiết bị xuất chuẩn là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
603, thì thuộc tính này là đối tượng luồng có thể đọc được khi được trả về bởi
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
664. Đọc từ luồng cung cấp đầu ra từ tiến trình con. Nếu đối số mã hóa hoặc lỗi đã được chỉ định hoặc đối số universal_newlines là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
602, thì luồng là luồng văn bản, nếu không thì luồng đó là luồng byte. Nếu đối số thiết bị xuất chuẩn không phải là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
603, thì thuộc tính này là
os.system
os.spawn*
94

Giáo hoàng. stderr

Nếu đối số stderr là

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
603, thì thuộc tính này là đối tượng luồng có thể đọc được khi được trả về bởi
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
664. Đọc từ luồng cung cấp đầu ra lỗi từ tiến trình con. Nếu đối số mã hóa hoặc lỗi đã được chỉ định hoặc đối số universal_newlines là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
602, thì luồng là luồng văn bản, nếu không thì luồng đó là luồng byte. Nếu đối số stderr không phải là
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
603, thì thuộc tính này là
os.system
os.spawn*
94

Cảnh báo

Sử dụng

os.system
os.spawn*
1964 thay vì
os.system
os.spawn*
1993,
os.system
os.spawn*
1994 hoặc
os.system
os.spawn*
1995 để tránh bế tắc do bất kỳ bộ đệm ống hệ điều hành nào khác lấp đầy và chặn tiến trình con

Giáo hoàng. pid

ID tiến trình của tiến trình con

Lưu ý rằng nếu bạn đặt đối số trình bao thành

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
602, thì đây là ID tiến trình của trình bao được sinh ra

Giáo hoàng. mã trả về

Mã trả về con, được đặt bởi ________ 11997 n ________ 11998 (và gián tiếp bởi ________ 11964). Giá trị

os.system
os.spawn*
94 cho biết quá trình chưa kết thúc

A negative value

os.system
os.spawn*
99 indicates that the child was terminated by signal
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
90 (POSIX only)

Trình trợ giúp Windows Popen¶

Lớp

os.system
os.spawn*
1909 và các hằng số sau đây chỉ khả dụng trên Windows

lớp quy trình con. STARTUPINFO(* , dwFlags=0, hStdInput=None, hStdOutput=None, hStdError=None, wShowWindow=0, lpAttributeList=None)

Hỗ trợ một phần cấu trúc Windows STARTUPINFO được sử dụng để tạo

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9. Các thuộc tính sau có thể được đặt bằng cách chuyển chúng dưới dạng đối số chỉ từ khóa

Đã thay đổi trong phiên bản 3. 7. Hỗ trợ đối số chỉ từ khóa đã được thêm vào.

dwFlags

Trường bit xác định xem một số thuộc tính

os.system
os.spawn*
1909 nhất định có được sử dụng khi quy trình tạo cửa sổ hay không

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0

hStdInput

Nếu

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
06 chỉ định
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
07, thuộc tính này là điều khiển đầu vào tiêu chuẩn cho quy trình. Nếu
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
07 không được chỉ định, mặc định cho đầu vào tiêu chuẩn là bộ đệm bàn phím

hStdOutput

Nếu

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
06 chỉ định
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
07, thuộc tính này là điều khiển đầu ra tiêu chuẩn cho quy trình. Mặt khác, thuộc tính này bị bỏ qua và mặc định cho đầu ra tiêu chuẩn là bộ đệm của cửa sổ giao diện điều khiển

hStdError

Nếu

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
06 chỉ định
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
07, thuộc tính này là xử lý lỗi tiêu chuẩn cho quy trình. Mặt khác, thuộc tính này bị bỏ qua và mặc định cho lỗi tiêu chuẩn là bộ đệm của cửa sổ giao diện điều khiển

wShowWindow

Nếu

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
06 chỉ định
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
14, thì thuộc tính này có thể là bất kỳ giá trị nào có thể được chỉ định trong tham số
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
15 cho hàm ShowWindow, ngoại trừ
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
16. Mặt khác, thuộc tính này bị bỏ qua

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
17 được cung cấp cho thuộc tính này. Nó được sử dụng khi
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 được gọi với
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
633

lpAttributeList

Từ điển các thuộc tính bổ sung để tạo quy trình như được đưa ra trong

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
20, xem UpdateProcThreadAttribute

Thuộc tính được hỗ trợ

handle_list

Trình tự xử lý sẽ được kế thừa. close_fds phải đúng nếu không trống

Các tay cầm phải tạm thời được kế thừa bởi

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
21 khi được chuyển đến hàm tạo
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9, nếu không thì
os.system
os.spawn*
1936 sẽ xuất hiện với lỗi Windows
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
24 (87)

Cảnh báo

Trong quy trình đa luồng, hãy thận trọng để tránh rò rỉ các thẻ điều khiển được đánh dấu là có thể kế thừa khi kết hợp tính năng này với các lệnh gọi đồng thời đến các chức năng tạo quy trình khác kế thừa tất cả các thẻ điều khiển, chẳng hạn như

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
25. Điều này cũng áp dụng cho chuyển hướng xử lý tiêu chuẩn, tạm thời tạo ra các xử lý kế thừa

Mới trong phiên bản 3. 7

Hằng Windows¶

Mô-đun

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2 hiển thị các hằng số sau

quy trình con. STD_INPUT_HANDLE

Thiết bị đầu vào tiêu chuẩn. Ban đầu, đây là bộ đệm đầu vào của bảng điều khiển,

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
27

quy trình con. STD_OUTPUT_HANDLE

Thiết bị đầu ra tiêu chuẩn. Ban đầu, đây là bộ đệm màn hình bảng điều khiển đang hoạt động,

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
28

quy trình con. STD_ERROR_HANDLE

Thiết bị báo lỗi tiêu chuẩn. Ban đầu, đây là bộ đệm màn hình bảng điều khiển đang hoạt động,

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
28

quy trình con. SW_HIDE

Ẩn cửa sổ. Một cửa sổ khác sẽ được kích hoạt

quy trình con. STARTF_USESTDHANDLES

Chỉ định rằng các thuộc tính

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
30,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
31 và
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
32 chứa thông tin bổ sung

quy trình con. STARTF_USESHOWWINDOW

Chỉ định rằng thuộc tính

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
33 chứa thông tin bổ sung

quy trình con. CREATE_NEW_CONSOLE

Quy trình mới có bảng điều khiển mới, thay vì kế thừa bảng điều khiển của cha mẹ nó (mặc định)

quy trình con. CREATE_NEW_PROCESS_GROUP

Tham số

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
35 để chỉ định rằng một nhóm quy trình mới sẽ được tạo. Cờ này là cần thiết để sử dụng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
36 trên quy trình con

Cờ này bị bỏ qua nếu

os.system
os.spawn*
1911 được chỉ định

quy trình con. ABOVE_NORMAL_PRIORITY_CLASS

Tham số

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
35 để chỉ định rằng một quy trình mới sẽ có mức độ ưu tiên trên mức trung bình

Mới trong phiên bản 3. 7

quy trình con. BELOW_NORMAL_PRIORITY_CLASS

Tham số

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
35 để chỉ định rằng một quy trình mới sẽ có mức độ ưu tiên dưới mức trung bình

Mới trong phiên bản 3. 7

quy trình con. HIGH_PRIORITY_CLASS

Tham số

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
35 để xác định rằng quy trình mới sẽ có mức độ ưu tiên cao

Mới trong phiên bản 3. 7

quy trình con. IDLE_PRIORITY_CLASS

Tham số

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
35 để chỉ định rằng quy trình mới sẽ có mức ưu tiên nhàn rỗi (thấp nhất)

Mới trong phiên bản 3. 7

quy trình con. NORMAL_PRIORITY_CLASS

Tham số

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
35 để chỉ định rằng một quy trình mới sẽ có mức độ ưu tiên bình thường. (mặc định)

Mới trong phiên bản 3. 7

quy trình con. REALTIME_PRIORITY_CLASS

Tham số

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
35 để chỉ định rằng quy trình mới sẽ có mức độ ưu tiên theo thời gian thực. Bạn gần như không bao giờ nên sử dụng REALTIME_PRIORITY_CLASS, vì điều này làm gián đoạn các luồng hệ thống quản lý đầu vào chuột, đầu vào bàn phím và xóa đĩa nền. Lớp này có thể phù hợp với các ứng dụng “nói chuyện” trực tiếp với phần cứng hoặc thực hiện các tác vụ ngắn cần hạn chế bị gián đoạn

Mới trong phiên bản 3. 7

quy trình con. CREATE_NO_WINDOW

Tham số

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
35 để xác định rằng quy trình mới sẽ không tạo cửa sổ

Mới trong phiên bản 3. 7

quy trình con. DETACHED_PROCESS

Tham số

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
35 để chỉ định rằng một quy trình mới sẽ không kế thừa bảng điều khiển của cha mẹ nó. Không thể sử dụng giá trị này với CREATE_NEW_CONSOLE

Mới trong phiên bản 3. 7

quy trình con. CREATE_DEFAULT_ERROR_MODE

Tham số

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
35 để chỉ định rằng quy trình mới không kế thừa chế độ lỗi của quy trình gọi. Thay vào đó, quy trình mới có chế độ lỗi mặc định. This feature is particularly useful for multithreaded shell applications that run with hard errors disabled

Mới trong phiên bản 3. 7

quy trình con. CREATE_BREAKAWAY_FROM_JOB

Một tham số

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
35 để xác định rằng một quy trình mới không được liên kết với công việc

Mới trong phiên bản 3. 7

API cấp cao cũ hơn¶

Trước Python 3. 5, ba chức năng này bao gồm API cấp cao để xử lý con. Bây giờ bạn có thể sử dụng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 trong nhiều trường hợp, nhưng rất nhiều mã hiện có gọi các hàm này

quy trình con. gọi(args , *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

Chạy lệnh được mô tả bởi args. Đợi lệnh hoàn thành, sau đó trả về thuộc tính

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
98

Mã cần nắm bắt thiết bị xuất chuẩn hoặc thiết bị xuất chuẩn nên sử dụng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 để thay thế

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9

Để triệt tiêu thiết bị xuất chuẩn hoặc thiết bị xuất chuẩn, hãy cung cấp giá trị

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
604

Các đối số hiển thị ở trên chỉ là một số đối số phổ biến. Chữ ký hàm đầy đủ giống như chữ ký của hàm tạo

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 - hàm này chuyển tất cả các đối số được cung cấp ngoài thời gian chờ trực tiếp đến giao diện đó

Note

Không sử dụng

Popen(['/bin/sh', '-c', args[0], args[1], ...])
3 hoặc
Popen(['/bin/sh', '-c', args[0], args[1], ...])
4 với chức năng này. Quá trình con sẽ bị chặn nếu nó tạo đủ đầu ra cho một đường ống để lấp đầy bộ đệm đường ống của hệ điều hành vì các đường ống không được đọc từ

Đã thay đổi trong phiên bản 3. 3. thời gian chờ đã được thêm.

quy trình con. check_call(args , *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

Chạy lệnh với đối số. Đợi lệnh hoàn thành. Nếu mã trả về bằng 0 thì trả về, nếu không thì tăng

os.system
os.spawn*
92. Đối tượng
os.system
os.spawn*
92 sẽ có mã trả về trong thuộc tính
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
98. Nếu
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
01 không thể bắt đầu quá trình, nó sẽ tuyên truyền ngoại lệ đã được nêu ra

Mã cần nắm bắt thiết bị xuất chuẩn hoặc thiết bị xuất chuẩn nên sử dụng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 để thay thế

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0

Để triệt tiêu thiết bị xuất chuẩn hoặc thiết bị xuất chuẩn, hãy cung cấp giá trị

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
604

Các đối số hiển thị ở trên chỉ là một số đối số phổ biến. Chữ ký hàm đầy đủ giống như chữ ký của hàm tạo

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
9 - hàm này chuyển tất cả các đối số được cung cấp ngoài thời gian chờ trực tiếp đến giao diện đó

Note

Không sử dụng

Popen(['/bin/sh', '-c', args[0], args[1], ...])
3 hoặc
Popen(['/bin/sh', '-c', args[0], args[1], ...])
4 với chức năng này. Quá trình con sẽ bị chặn nếu nó tạo đủ đầu ra cho một đường ống để lấp đầy bộ đệm đường ống của hệ điều hành vì các đường ống không được đọc từ

Đã thay đổi trong phiên bản 3. 3. thời gian chờ đã được thêm.

quy trình con. check_output(args , *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None, errors=None, universal_newlines=None, timeout=None, text=None, **other_popen_kwargs)

Chạy lệnh với các đối số và trả về đầu ra của nó

Nếu mã trả về khác 0, nó sẽ tăng

os.system
os.spawn*
92. Đối tượng
os.system
os.spawn*
92 sẽ có mã trả về trong thuộc tính
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
98 và bất kỳ đầu ra nào trong thuộc tính
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
77

Điều này tương đương với

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
60

Các đối số hiển thị ở trên chỉ là một số đối số phổ biến. Chữ ký chức năng đầy đủ phần lớn giống như chữ ký của

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 - hầu hết các đối số được chuyển trực tiếp qua giao diện đó. Tồn tại một sai lệch API so với hành vi
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8. chuyển
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
80 sẽ hoạt động giống như
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
81 (hoặc
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
82, tùy thuộc vào các đối số khác) thay vì sử dụng trình xử lý tệp đầu vào tiêu chuẩn của cha mẹ

Theo mặc định, chức năng này sẽ trả về dữ liệu dưới dạng byte được mã hóa. Mã hóa thực tế của dữ liệu đầu ra có thể phụ thuộc vào lệnh được gọi, do đó, việc giải mã thành văn bản thường sẽ cần được xử lý ở cấp ứng dụng

Hành vi này có thể bị ghi đè bằng cách đặt văn bản, mã hóa, lỗi hoặc universal_newlines thành

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
602 như được mô tả trong Đối số thường được sử dụng
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8.

Để ghi lại lỗi tiêu chuẩn trong kết quả, hãy sử dụng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
93

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
61

Mới trong phiên bản 3. 1

Đã thay đổi trong phiên bản 3. 3. thời gian chờ đã được thêm.

Đã thay đổi trong phiên bản 3. 4. Đã thêm hỗ trợ cho đối số từ khóa đầu vào.

Đã thay đổi trong phiên bản 3. 6. mã hóa và lỗi đã được thêm vào. Xem

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
8 để biết chi tiết.

Mới trong phiên bản 3. 7. văn bản đã được thêm dưới dạng bí danh dễ đọc hơn cho universal_newlines.

Thay thế các chức năng cũ hơn bằng Mô-đun >>> import shlex, subprocess >>> command_line = input() /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'" >>> args = shlex.split(command_line) >>> print(args) ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"] >>> p = subprocess.Popen(args) # Success! 2¶

Trong phần này, “a trở thành b” có nghĩa là b có thể được dùng để thay thế cho a

Note

Tất cả các hàm “a” trong phần này đều bị lỗi (ít nhiều) một cách âm thầm nếu không tìm thấy chương trình đã thực thi;

Ngoài ra, việc thay thế bằng cách sử dụng

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
07 sẽ không thành công với
os.system
os.spawn*
92 nếu thao tác được yêu cầu tạo ra mã trả về khác không. Đầu ra vẫn có sẵn dưới dạng thuộc tính
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
77 của ngoại lệ đã nêu

Trong các ví dụ sau, chúng tôi giả sử rằng các chức năng liên quan đã được nhập từ mô-đun

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2

Thay thế thay thế lệnh shell /bin/sh¶

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
62

trở thành

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
63

Thay thế đường ống dẫn vỏ¶

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
64

trở thành

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
65

Cuộc gọi

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
93 sau khi bắt đầu p2 rất quan trọng để p1 nhận được SIGPIPE nếu p2 thoát trước p1

Ngoài ra, đối với đầu vào đáng tin cậy, hỗ trợ đường ống riêng của trình bao vẫn có thể được sử dụng trực tiếp

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
64

trở thành

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
67

Thay thế >>> import shlex, subprocess >>> command_line = input() /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'" >>> args = shlex.split(command_line) >>> print(args) ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"] >>> p = subprocess.Popen(args) # Success! 25¶

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
68

ghi chú

  • Gọi chương trình thông qua trình bao thường không bắt buộc

  • Giá trị trả về của

    os.system
    os.spawn*
    
    1945 được mã hóa khác với giá trị của
    >>> import shlex, subprocess
    >>> command_line = input()
    /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
    >>> args = shlex.split(command_line)
    >>> print(args)
    ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
    >>> p = subprocess.Popen(args) # Success!
    
    25

  • Hàm

    >>> import shlex, subprocess
    >>> command_line = input()
    /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
    >>> args = shlex.split(command_line)
    >>> print(args)
    ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
    >>> p = subprocess.Popen(args) # Success!
    
    25 bỏ qua các tín hiệu SIGINT và SIGQUIT trong khi lệnh đang chạy, nhưng người gọi phải thực hiện việc này một cách riêng biệt khi sử dụng mô-đun
    >>> import shlex, subprocess
    >>> command_line = input()
    /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
    >>> args = shlex.split(command_line)
    >>> print(args)
    ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
    >>> p = subprocess.Popen(args) # Success!
    
    2

Một ví dụ thực tế hơn sẽ như thế này

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
69

Replacing the >>> import shlex, subprocess >>> command_line = input() /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'" >>> args = shlex.split(command_line) >>> print(args) ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"] >>> p = subprocess.Popen(args) # Success! 99 family¶

Ví dụ P_NOWAIT

os.system
os.spawn*
190

ví dụ P_WAIT

os.system
os.spawn*
191

Ví dụ véc tơ

os.system
os.spawn*
192

Ví dụ về môi trường

os.system
os.spawn*
193

Thay thế Popen(['/bin/sh', '-c', args[0], args[1], ...]) 00, Popen(['/bin/sh', '-c', args[0], args[1], ...]) 01, Popen(['/bin/sh', '-c', args[0], args[1], ...]) 02¶

os.system
os.spawn*
194

os.system
os.spawn*
195

os.system
os.spawn*
196

Xử lý mã trả về dịch như sau

os.system
os.spawn*
197

Thay thế các chức năng từ mô-đun Popen(['/bin/sh', '-c', args[0], args[1], ...]) 03¶

Note

Nếu đối số cmd cho các hàm popen2 là một chuỗi, thì lệnh được thực thi thông qua /bin/sh. Nếu nó là một danh sách, lệnh được thực hiện trực tiếp

os.system
os.spawn*
198

os.system
os.spawn*
199

Popen(['/bin/sh', '-c', args[0], args[1], ...])
04 và
Popen(['/bin/sh', '-c', args[0], args[1], ...])
05 về cơ bản hoạt động như
os.system
os.spawn*
1926, ngoại trừ điều đó

  • >>> import shlex, subprocess
    >>> command_line = input()
    /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
    >>> args = shlex.split(command_line)
    >>> print(args)
    ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
    >>> p = subprocess.Popen(args) # Success!
    
    9 đưa ra một ngoại lệ nếu thực thi không thành công

  • Đối số capturestderr được thay thế bằng đối số stderr

  • os.system
    os.spawn*
    
    91 và
    Popen(['/bin/sh', '-c', args[0], args[1], ...])
    
    3 phải được chỉ định

  • popen2 đóng tất cả các bộ mô tả tệp theo mặc định, nhưng bạn phải chỉ định

    Popen(['/bin/sh', '-c', args[0], args[1], ...])
    
    10 với
    >>> import shlex, subprocess
    >>> command_line = input()
    /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
    >>> args = shlex.split(command_line)
    >>> print(args)
    ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
    >>> p = subprocess.Popen(args) # Success!
    
    9 để đảm bảo hành vi này trên tất cả các nền tảng hoặc phiên bản Python trước đây

Chức năng triệu gọi Shell kế thừa¶

Mô-đun này cũng cung cấp các chức năng kế thừa sau từ 2. x mô-đun

Popen(['/bin/sh', '-c', args[0], args[1], ...])
12. Các hoạt động này gọi ngầm hệ thống và không có đảm bảo nào được mô tả ở trên về tính nhất quán xử lý ngoại lệ và bảo mật là hợp lệ đối với các chức năng này

quy trình con. getstatusoutput(cmd , *, encoding=None, errors=None)

Trả lại

Popen(['/bin/sh', '-c', args[0], args[1], ...])
13 khi thực thi cmd trong trình bao

Thực thi chuỗi cmd trong trình bao với

Popen(['/bin/sh', '-c', args[0], args[1], ...])
14 và trả về 2-tuple
Popen(['/bin/sh', '-c', args[0], args[1], ...])
13. mã hóa và lỗi được sử dụng để giải mã đầu ra; .
Frequently Used Arguments for more details.

A trailing newline is stripped from the output. Mã thoát của lệnh có thể được hiểu là mã trả về của quy trình con. Thí dụ

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
0

Tính khả dụng . Hệ điều hành Unix, Windows.

Đã thay đổi trong phiên bản 3. 3. 4. Hỗ trợ Windows đã được thêm vào.

Hàm hiện trả về (mã thoát, đầu ra) thay vì (trạng thái, đầu ra) như trong Python 3. 3. 3 và sớm hơn. mã thoát có cùng giá trị với

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
98

Mới trong phiên bản 3. 11. Đã thêm đối số mã hóa và lỗi.

quy trình con. getoutput(cmd , * , encoding=None , errors=None)

Return output (stdout and stderr) of executing cmd in a shell

Giống như

Popen(['/bin/sh', '-c', args[0], args[1], ...])
17, ngoại trừ mã thoát bị bỏ qua và giá trị trả về là một chuỗi chứa đầu ra của lệnh. Thí dụ

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
1

Tính khả dụng . Hệ điều hành Unix, Windows.

Changed in version 3. 3. 4. Đã thêm hỗ trợ Windows

Mới trong phiên bản 3. 11. Đã thêm đối số mã hóa và lỗi.

Ghi chú¶

Chuyển đổi một chuỗi đối số thành một chuỗi trên Windows¶

Trên Windows, một chuỗi args được chuyển đổi thành một chuỗi có thể được phân tích cú pháp bằng các quy tắc sau (tương ứng với các quy tắc được sử dụng bởi thời gian chạy MS C)

  1. Các đối số được phân định bằng khoảng trắng, là khoảng trắng hoặc tab

  2. Một chuỗi được bao quanh bởi dấu ngoặc kép được hiểu là một đối số, bất kể khoảng trắng có trong đó. Một chuỗi được trích dẫn có thể được nhúng trong một đối số

  3. A double quotation mark preceded by a backslash is interpreted as a literal double quotation mark

  4. Backslashes are interpreted literally, unless they immediately precede a double quotation mark

  5. If backslashes immediately precede a double quotation mark, every pair of backslashes is interpreted as a literal backslash. Nếu số dấu gạch chéo ngược là số lẻ, dấu gạch chéo ngược cuối cùng sẽ thoát khỏi dấu ngoặc kép tiếp theo như được mô tả trong quy tắc 3

Xem thêm

Popen(['/bin/sh', '-c', args[0], args[1], ...])
18

Mô-đun cung cấp chức năng phân tích cú pháp và thoát khỏi các dòng lệnh

Vô hiệu hóa việc sử dụng Popen(['/bin/sh', '-c', args[0], args[1], ...]) 19 hoặc Popen(['/bin/sh', '-c', args[0], args[1], ...]) 20¶

On Linux,

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
2 defaults to using the
Popen(['/bin/sh', '-c', args[0], args[1], ...])
19 system call internally when it is safe to do so rather than
Popen(['/bin/sh', '-c', args[0], args[1], ...])
23. This greatly improves performance

Nếu bạn từng gặp phải một tình huống được cho là rất bất thường khi bạn cần ngăn không cho Python sử dụng

Popen(['/bin/sh', '-c', args[0], args[1], ...])
19, bạn có thể đặt thuộc tính
Popen(['/bin/sh', '-c', args[0], args[1], ...])
25 thành giá trị sai

subprocess. _USE_VFORK = False # See CPython issue gh-NNNNNN

Setting this has no impact on use of

Popen(['/bin/sh', '-c', args[0], args[1], ...])
20 which could use
Popen(['/bin/sh', '-c', args[0], args[1], ...])
19 internally within its libc implementation. There is a similar
Popen(['/bin/sh', '-c', args[0], args[1], ...])
28 attribute if you need to prevent use of that

subprocess. _USE_POSIX_SPAWN = False # See CPython issue gh-NNNNNN

It is safe to set these to false on any Python version. Chúng sẽ không có tác dụng trên các phiên bản cũ hơn khi không được hỗ trợ. Do not assume the attributes are available to read. Despite their names, a true value does not indicate that the corresponding function will be used, only that that it may be

Vui lòng gửi các sự cố bất cứ khi nào bạn phải sử dụng các nút riêng tư này để tái tạo sự cố mà bạn đang gặp phải. Link to that issue from a comment in your code