Hướng dẫn python subprocess run executable linux - quy trình con python chạy linux thực thi

Để thực hiện chương trình bên ngoài, hãy làm điều này:

Show
import subprocess
args = ("bin/bar", "-c", "somefile.xml", "-d", "text.txt", "-r", "aString", "-f", "anotherString")
#Or just:
#args = "bin/bar -c somefile.xml -d text.txt -r aString -f anotherString".split()
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
print output

Và vâng, giả sử chương trình

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
9 của bạn đã viết một số tệp khác cho đĩa, bạn có thể mở chúng như bình thườ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!
0. Lưu ý rằng bạn không cần phải dựa vào Subshell để chuyển hướng đầu ra sang tệp trên đĩa có tên là "đầu ra" nếu bạn không muốn. Tôi đang trình bày ở đây cách đọc trực tiếp đầu ra vào chương trình Python của bạn mà không cần đến đĩa ở giữa.

Bước 5: Chạy thực thi .. Lib/subprocess.py


Làm cách nào để chạy lệnh shell trong Python bằng cách sử dụng quy trình con?

Chức năng Chức năng Run Run () của Python đã được thêm vào Python 3.5 và nên sử dụng hàm Run () để thực thi các lệnh shell trong chương trình Python. Đối số ARGS trong quy trình con. Chức năng chạy () lấy lệnh shell và trả về một đối tượng hoàn thành trong Python.

Các đối số được phân định bởi không gian trắng, đó là không gian hoặc một tab.

Một chuỗi được bao quanh bởi các dấu ngoặc kép được hiểu là một đối số duy nhất, bất kể không gian trắng có trong. Một chuỗi được trích dẫn có thể được nhúng trong một đối số. – PEP proposing the subprocess module

Một dấu ngoặc kép có trước một dấu gạch chéo ngược được hiểu là một dấu ngoặc kép theo nghĩa đen.

Backslashes được giải thích theo nghĩa đen, trừ khi chúng ngay lập tức đi trước dấu ngoặc kép.

Nếu dấu gạch chéo ngược ngay trước dấu ngoặc kép, mỗi cặp dấu gạch chéo ngược được hiểu là một dấu gạch chéo ngược theo nghĩa đen. Nếu số lượng 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.(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)

Xem thêm

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
03Frequently 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!
5 constructor - most of the arguments to this function are passed through to that interface. (timeout, input, check, and capture_output are not.)

Nếu Capture_output là đúng, stdout và stderr sẽ bị bắt. Khi được sử dụng, đối 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!
5 nội bộ được tự động tạo bằng
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 và
Popen(['/bin/sh', '-c', args[0], args[1], ...])
2. Các đối số stdout và stderr có thể không được cung cấp cùng lúc với Capture_output. Nếu bạn muốn chụp và kết hợp cả hai luồng thành một, hãy sử dụng
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 và
Popen(['/bin/sh', '-c', args[0], args[1], ...])
4 thay vì Capture_output.

Đối số thời gian chờ được chuyển đến

Popen(['/bin/sh', '-c', args[0], args[1], ...])
5. Nếu thời gian chờ hết hạn, quá trình trẻ em sẽ bị giết và chờ đợi. Ngoại lệ
Popen(['/bin/sh', '-c', args[0], args[1], ...])
6 sẽ được nuôi dưỡng lại sau khi quá trình con đã chấm dứt.

Đối số đầu vào được chuyển đến

Popen(['/bin/sh', '-c', args[0], args[1], ...])
5 và do đó cho quá trình phụ stdin. Nếu được sử dụng, nó phải là một chuỗi byte hoặc một chuỗi nếu mã hóa hoặc lỗi được chỉ định hoặc văn bản là đúng. Khi được sử dụng, đối 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!
5 nội bộ được tự động tạo bằng
Popen(['/bin/sh', '-c', args[0], args[1], ...])
9 và đối số Stdin cũng không được sử dụng.

Nếu kiểm tra là đúng và quy trình thoát ra với mã thoát khác không, ngoại lệ

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
0 sẽ được nêu ra. Các thuộc tính của ngoại lệ đó giữ các đối số, mã thoát và stdout và stderr nếu chúng bị bắt.

Nếu mã hóa hoặc lỗi được chỉ định hoặc văn bản là đúng, các đối tượng tệp cho stdin, stdout và stderr được mở ở chế độ văn bản bằng cách sử dụng mã hóa và lỗi được chỉ định hoặc mặc định

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
1. Đối số Universal_Newlines tương đương với văn bản và được cung cấp cho khả năng tương thích ngược. Theo mặc định, các đối tượng tệp được mở ở chế độ nhị phân.

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

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, thì đó phải là một ánh xạ xác định các biến môi trường cho quy trình mới; Chúng được sử dụng thay vì hành vi mặc định của việc kế thừa môi trường quy trình hiện tại. Nó được truyền trực tiếp đế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!
5.

Examples:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')

Mới trong phiên bản 3.5.

Đã thay đổi trong phiên bản 3.6: Đã thêm thông số mã hóa và lỗiAdded encoding and errors parameters

Đã thay đổi trong phiên bản 3.7: Đã thêm tham số văn bản, như một bí danh dễ hiểu hơn của Universal_Newlines. Đã thêm tham số Capture_output.Added the text parameter, as a more understandable alias of universal_newlines. Added the capture_output parameter.

Lớp ________ 36 ________ 55¶

Giá trị trả về 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!
4, đại diện cho một quá trình đã kết thúc.

________ 57¶

Các đối số được sử dụng để khởi chạy quá trình. Đây có thể là một danh sách hoặc một chuỗi.

________ 58¶

Trạng thái thoát của quá trình trẻ em. Thông thường, trạng thái thoát là 0 chỉ ra rằng nó đã chạy thành công.

Giá trị âm

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
9 chỉ ra rằng đứa trẻ bị chấm dứt bởi tín hiệu
proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
0 (chỉ POSIX).

________ 61¶

Đã bắt được stdout từ quá trình trẻ em. Trình tự byte hoặc chuỗi 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!
4 được gọi với mã hóa, lỗi hoặc văn bản = true.
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2 nếu stdout không bị bắt.

Nếu bạn chạy quy trình với

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
4, stdout và stderr sẽ được kết hợp trong thuộc tính này và
proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
5 sẽ là
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2.

________ 67¶

Đã bắt Stderr từ quá trình trẻ em. Trình tự byte hoặc chuỗi 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!
4 được gọi với mã hóa, lỗi hoặc văn bản = true.
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2 Nếu Stderr không bị bắt.

________ 70 ()()

Nếu

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1 là không khác, hãy tăng
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
0.

Mới trong phiên bản 3.5.

Đã thay đổi trong phiên bản 3.6: Đã thêm thông số mã hóa và lỗi

Đã thay đổi trong phiên bản 3.7: Đã thêm tham số văn bản, như một bí danh dễ hiểu hơn của Universal_Newlines. Đã thêm tham số Capture_output.

Lớp ________ 36 ________ 55¶

Giá trị trả về 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!
4, đại diện cho một quá trình đã kết thúc.

________ 57¶

Các đối số được sử dụng để khởi chạy quá trình. Đây có thể là một danh sách hoặc một chuỗi.

________ 58¶

Trạng thái thoát của quá trình trẻ em. Thông thường, trạng thái thoát là 0 chỉ ra rằng nó đã chạy thành công.

Giá trị âm

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
9 chỉ ra rằng đứa trẻ bị chấm dứt bởi tín hiệu
proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
0 (chỉ POSIX).

Lớp ________ 36 ________ 55¶

Giá trị trả về 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!
4, đại diện cho một quá trình đã kết thúc.

________ 57¶

Các đối số được sử dụng để khởi chạy quá trình. Đây có thể là một danh sách hoặc một chuỗi.

________ 58¶

Trạng thái thoát của quá trình trẻ em. Thông thường, trạng thái thoát là 0 chỉ ra rằng nó đã chạy thành công.

Giá trị âm

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
9 chỉ ra rằng đứa trẻ bị chấm dứt bởi tín hiệu
proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
0 (chỉ POSIX).

________ 61¶

Đã bắt được stdout từ quá trình trẻ em. Trình tự byte hoặc chuỗi 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!
4 được gọi với mã hóa, lỗi hoặc văn bản = true.
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2 nếu stdout không bị bắt.

________ 61¶

Đã bắt được stdout từ quá trình trẻ em. Trình tự byte hoặc chuỗi 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!
4 được gọi với mã hóa, lỗi hoặc văn bản = true.
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2 nếu stdout không bị bắt.

________ 67¶

Đã bắt Stderr từ quá trình trẻ em. Trình tự byte hoặc chuỗi 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!
4 được gọi với mã hóa, lỗi hoặc văn bản = true.
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2 Nếu Stderr không bị bắt.

Lớp ________ 36 ________ 55¶

Giá trị trả về 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!
4, đại diện cho một quá trình đã kết thúc.stdout and stderr attributes added

________ 57¶

Các đối số được sử dụng để khởi chạy quá trình. Đây có thể là một danh sách hoặc một chuỗi.

________ 58¶

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

________ 89¶

Lệnh được sử dụng để sinh ra quá trình trẻ em.

________ 91¶

Đầu ra của quá trình con nếu nó bị bắt 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!
4 hoặc
>>> subprocess.check_output(
...     "ls non_existent_file; exit 0",
...     stderr=subprocess.STDOUT,
...     shell=True)
'ls: non_existent_file: No such file or directory\n'
3. Nếu không,
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2.

________ 61¶

Bí danh cho đầu ra, để đối xứng với

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()
5.

________ 67¶

Đầu ra Stderr của quá trình con nếu nó bị bắt 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!
4. Nếu không,
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2.

Đã thay đổi trong Phiên bản 3.5: Thuộc tính Stdout và Stderr được thêmstdout and stderr attributes added

Các đố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!
5 (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ố này có thể được để lại một cách an toàn tại các giá trị mặc định của chúng. Các đối số phổ biến nhất là:

Args là bắt buộc cho 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ố chương trình. Cung cấp một chuỗi các đối số thường được ưa thích, vì nó cho phép mô -đun chăm sóc bất kỳ việc thoát ra và trích dẫn các đối số cần thiết nào (ví dụ: cho phép không gian trong tên tệp). Nếu truyền một chuỗi duy nhất, một trong hai shell phải là

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
27 (xem bên dưới) hoặc nếu không thì chuỗi chỉ cần đặt tên cho chương trình sẽ được thực thi mà không cần chỉ định bất kỳ đối số nào.

Stdin, stdout và stderr chỉ định đầu vào tiêu chuẩn của chương trình, đầu ra tiêu chuẩn và tay cầm tệp tiêu chuẩn, tương ứng. Các giá trị hợp lệ là

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
28,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
29, một bộ mô tả tệp hiện có (một số nguyên dương), một đối tượng tệp hiện có với bộ mô tả tệp hợp lệ và
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2.
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
28 chỉ ra rằng một đường ống mới cho trẻ nên được tạo ra.
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
29 chỉ ra rằng tệp đặc biệt
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
6 sẽ được sử dụng. Với các cài đặt mặc định của
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, sẽ không có sự chuyển hướng nào xảy ra; Tay cầm tập tin trẻ con sẽ được kế thừa từ cha mẹ. Ngoài ra, STDERR có thể là
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
35, điều này chỉ ra rằng dữ liệu STDERR từ quy trình con phải được ghi vào cùng một tay cầm tệp như đối với stdout.

Nếu mã hóa hoặc lỗi được chỉ định hoặc văn bản (còn được gọi là Universal_Newlines) là đúng, các đối tượng tệp STDIN, STDOUT và STDERR sẽ được mở ở chế độ văn bản bằng cách sử dụng mã hóa và lỗi được chỉ định trong cuộc gọi hoặc mặc định cho

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
1.

Đối với STDIN, các ký tự kết thúc dòng

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
37 trong đầu vào sẽ được chuyển đổi thành dấu phân cách dòng mặc định
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
38. Đối với stdout và stderr, tất cả các kết thúc dòng trong đầu ra sẽ được chuyển đổi thành
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
37. Để biết thêm thông tin, hãy xem tài liệu của lớp
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
1 khi đối số dòng mới cho hàm tạo của nó là
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2.

Nếu chế độ văn bản không được sử dụng, stdin, stdout và stderr sẽ được mở dưới dạng các luồng nhị phân. Không có chuyển đổi mã hóa hoặc kết thúc dòng được thực hiện.

Mới trong phiên bản 3.6: Đã thêm các tham số mã hóa và lỗi.Added encoding and errors parameters.

Mới trong phiên bản 3.7: Đã thêm tham số văn bản dưới dạng bí danh cho Universal_Newlines.Added the text parameter as an alias for universal_newlines.

Nếu shell là

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
27, lệnh được chỉ định sẽ được thực thi thông qua shell. Điều này có thể hữu ích nếu bạn đang sử dụng Python chủ yếu cho luồng điều khiển nâng cao mà nó cung cấp trên hầu hết các vỏ hệ thống và vẫn muốn truy cập thuận tiện vào các tính năng vỏ khác như ống shell, ký tự đại diện, mở rộng biến đổi môi trường và mở rộng
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
43 cho nhà của người dùng danh mục. Tuy nhiên, lưu ý rằng bản thân Python cung cấp việc triển khai nhiều tính năng giống như vỏ (đặc biệt là
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
44,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
45,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
46,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
47,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
48 và
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
49).

Đã thay đổi trong phiên bản 3.3: Khi Universal_Newlines là

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
27, lớp sử dụng mã hóa
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
51 thay vì
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
52. Xem lớp
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
1 để biết thêm thông tin về thay đổi này.When universal_newlines is
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
27, the class uses the encoding
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
51 instead of
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
52. See the
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
1 class for more information on this change.

Các tùy chọn này, cùng với tất cả các tùy chọn khác, được mô tả chi tiết hơn trong tài liệu xây 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!
5.

Constructor Popen Constructor¶

Việc tạo và quản lý quy trình cơ bản trong mô -đun này được xử lý bởi 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!
5. Nó cung cấp rất nhiều sự linh hoạt để các nhà phát triển có thể xử lý các trường hợp ít phổ biến hơn không được bao phủ bởi các chức năng tiện lợi.

Lớp ________ 36 ________ 157 (args, bufsize =- 1, thực thi = none, stdin = none, stdout = none, stderr = none startupInfo = none, creationflags = 0, restore_signals = true, start_new_session = false, pass_fds = (), *, nhóm = none Không, đường ống =- 1) ¶(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)

Thực hiện một chương trình trẻ em trong một quy trình mới. Trên Posix, lớp sử dụng hành vi giống như ____ 158 để thực hiện chương trình trẻ em. Trên Windows, lớp sử dụng chức năng Windows

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
59. Các đối số 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!
5 như sau.

Args nên là một chuỗi các đối số chương trình hoặc một đối tượng giống như một chuỗi hoặc giống như đường dẫn. Theo mặc định, chương trình thực thi là mục đầu tiên trong args nếu args là một chuỗi. Nếu Args là một chuỗi, việc giải thích phụ thuộc vào nền tảng và được mô tả dưới đây. Xem các đối số shell và thực thi để biết sự khác biệt bổ sung so với hành vi mặc định. Trừ khi có quy định khác, nên truyền ARGS như một chuỗi.path-like object. By default, the program to execute is the first item in args if args is a sequence. If args is a string, the interpretation is platform-dependent and described below. See the shell and executable arguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to pass args as a sequence.

Cảnh báo

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

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
61, hãy sử dụng
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
62. Trên tất cả các nền tảng, việc vượt qua
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
63 là cách được đề xuất để 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
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
64 để khởi chạy mô-đun đã cài đặt.
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
61, use
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
62. On all platforms, passing
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
63 is the recommended way to launch the current Python interpreter again, and use the
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
64 command-line format to launch an installed module.

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

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
58 và lưu ý rằng khi giải quyết hoặc tìm kiếm đường dẫn thực thi, CWD ghi đè thư mục làm việc hiện tại và ENV có thể ghi đè biến môi trường
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
61. Đối với Windows, hãy xem tài liệu của các tham số
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
67 và
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
68 của WinAPI
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
69 và lưu ý rằng khi giải quyết hoặc tìm kiếm đường dẫn thực thi với
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
70, CWD không ghi đè lên thư mục làm việc hiện tại và Env không thể ghi đè lên biến đổi môi trường
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
61. Sử dụng một đường dẫn đầy đủ tránh 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 như một chuỗi là:

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])

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

Ghi chú

Có thể không rõ ràng làm thế nào để chia một 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.

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
72 có thể minh họa cách xác định mã thông báo chính xác cho ARGS:

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

Lưu ý cụ thể rằng các tùy chọn (chẳng hạn như -input) và các đối số (như trứng.txt) được phân tách bằng khoảng trắng trong shell đi trong các yếu tố danh sách riêng biệt, trong khi các đối số cần trích dẫn hoặc rút lại khi sử dụng Tên tệp chứa khoảng trắng hoặc lệnh Echo được hiển thị ở trên) là các yếu tố danh sách đơn.

Trên Windows, nếu Args là một chuỗi, nó sẽ được chuyển đổi thành một chuỗi theo cách được mô tả trong việc chuyển đổi một chuỗi đối số thành một chuỗi trên Windows. Điều này là do

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
59 cơ bản hoạt động trên các chuỗi.Converting an argument sequence to a string on Windows. This is because the underlying
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
59 operates on strings.

Đã thay đổi trong phiên bản 3.6: Tham số Args chấp nhận một đối tượng giống như đường dẫn nếu shell là

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
74 và một chuỗi chứa các đối tượng giống như đường dẫn trên POSIX.args parameter accepts a path-like object if shell is
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
74 and a sequence containing path-like objects on POSIX.

Thay đổi trong phiên bản 3.8: Tham số Args chấp nhận một đối tượng giống như đường dẫn nếu shell là

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
74 và một chuỗi chứa byte và các đối tượng giống như đường dẫn trên Windows.args parameter accepts a path-like object if shell is
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
74 and a sequence containing bytes and path-like objects on Windows.

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

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
74) chỉ định xem có nên sử dụng shell làm chương trình để thực thi hay không. Nếu shell là
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
27, nên chuyển Args dưới dạng chuỗi chứ không phải là một chuỗi.

Trên POSIX với

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
78, shell mặc định là
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
79. Nếu args là một chuỗi, chuỗi chỉ định lệnh để thực thi thông qua shell. Điều này có nghĩa là chuỗi phải được định dạng chính xác như khi được gõ tại dấu nhắc shell. Điều này bao gồm, ví dụ, trích dẫn hoặc chao đảo thoát khỏi các tên tệp với không gian trong đó. Nếu Args 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 shell. Đ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!
5 có tương đương với:

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

Trên Windows với

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
78, biến môi trường
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
82 chỉ định shell mặc định. Lần duy nhất bạn cần chỉ định
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
78 trên Windows là khi lệnh bạn muốn thực thi được tích hợp vào shell (ví dụ: dir hoặc sao chép). Bạn không cần
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
78 để chạy tệp hàng loạt hoặc thực thi dựa trên bảng điều khiển.
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
82 environment variable specifies the default shell. The only time you need to specify
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
78 on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
78 to run a batch file or console-based executable.

Bufsize sẽ được cung cấp dưới dạng đối số tương ứng cho hàm

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
85 khi tạo các đối tượng tệp ống stdin/stdout/stderr:

  • >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    86 có nghĩa là không bị ảnh hưởng (đọc và ghi là một cuộc gọi hệ thống và có thể quay lại ngắn)

  • >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    87 có nghĩa là bộ đệm dòng (chỉ có thể sử dụng nếu
    >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
    
    88 tức là, trong 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 có mặc định thành -1 để bật 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 là

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
86 được không bị truy cập và cho phép đọc ngắn. Điều này là vô ý 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.bufsize now defaults to -1 to enable buffering by default to match the behavior that most code expects. In versions prior to Python 3.2.4 and 3.3.1 it incorrectly defaulted to
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
86 which was unbuffered and allowed short reads. This was unintentional and did not match the behavior of Python 2 as most code expected.

Đố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

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
70, thực thi có thể thay thế chương trình để thực hiện được chỉ định bởi Args. Tuy nhiên, ARG ban đầu vẫn được chuyển cho chương trình. Hầu hết các chương trình coi chương trình được ARG chỉ định là tên lệnh, sau đó có thể khác với chương trình thực sự được thực hiện. Trên Posix, tên ARGS trở thành tên hiển thị cho các tiện ích có thể thực thi như PS. Nếu
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
78, trên POSIX đối số thực thi chỉ định một shell thay thế cho
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
79 mặc định.ps. If
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
78, on POSIX the executable argument specifies a replacement shell for the default
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
79.

Thay đổi trong phiên bản 3.6: Tham số thực thi chấp nhận một đối tượng giống như đường dẫn trên POSIX.executable parameter accepts a path-like object on POSIX.

Thay đổi trong phiên bản 3.8: Tham số thực thi chấp nhận một byte và đối tượng giống như đường dẫn trên Windows.executable parameter accepts a bytes and path-like object on Windows.

Stdin, stdout và stderr chỉ định đầu vào tiêu chuẩn của chương trình, đầu ra tiêu chuẩn và tay cầm tệp tiêu chuẩn, tương ứng. Các giá trị hợp lệ là

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
28,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
29, một bộ mô tả tệp hiện có (một số nguyên dương), một đối tượng tệp hiện có với bộ mô tả tệp hợp lệ và
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2.
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
28 chỉ ra rằng một đường ống mới cho trẻ nên được tạo ra.
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
29 chỉ ra rằng tệp đặc biệt
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
6 sẽ được sử dụng. Với các cài đặt mặc định của
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, sẽ không có sự chuyển hướng nào xảy ra; Tay cầm tập tin trẻ con sẽ được kế thừa từ cha mẹ. Ngoài ra, STDERR có thể là
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
35, điều này chỉ ra rằng dữ liệu STDERR từ các ứng dụng phải được ghi vào cùng một tay cầm tệp như đối với stdout.file object with a valid file descriptor, and
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2.
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
28 indicates that a new pipe to the child should be created.
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
29 indicates that the special file
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
6 will be used. With the default settings of
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, no redirection will occur; the child’s file handles will be inherited from the parent. Additionally, stderr can be
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
35, which indicates that the stderr data from the applications should be captured into the same file handle as for stdout.

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

Cảnh báo

Tham số PEREXEC_FN không an toàn để sử dụng với sự hiện diện của các luồng trong ứng dụng của bạn. Quá trình trẻ em có thể bế tắc trước khi EXEC được gọi. Nếu bạn phải sử dụng nó, hãy giữ nó tầm thường! Giảm thiểu số lượng thư viện bạn gọi vào.

Ghi chú

Nếu bạn cần sửa đổi môi trường cho đứa trẻ sử dụng tham số env thay vì thực hiện nó trong preexec_fn. Tham số start_new_session có thể thay thế việc sử dụng phổ biến trước đây của preexec_fn để gọi os.setsid () trong trẻ.

Đã thay đổi trong phiên bản 3.8: Tham số PreExec_FN không còn được hỗ trợ trong các phần phụ. Việc sử dụng tham số trong trình điều khiển phụ tăng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
01. 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.The preexec_fn parameter is no longer supported in subinterpreters. The use of the parameter in a subinterpreter raises
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
01. The new restriction may affect applications that are deployed in mod_wsgi, uWSGI, and other embedded environments.

Nếu Close_FDS là đúng, tất cả các mô tả tệp ngoại trừ

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
86,
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
87 và
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
04 sẽ được đóng trước khi quá trình con được thực thi. Mặt khác, khi Close_FDS là sai, các mô tả tệp tuân theo cờ di truyền của họ như được mô tả trong kế thừa của các mô tả tệp.Inheritance of File Descriptors.

Trên Windows, nếu Close_FDS là đúng thì sẽ không có tay cầm nào được kế thừa bằng quy trình con trừ khi được thông qua rõ ràng trong phần tử

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
05 của
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
06 hoặc theo chuyển hướng xử lý tiêu chuẩn.

Đã thay đổi trong phiên bản 3.2: Mặc định cho Close_FDS đã được thay đổi từ

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
74 thành những gì được mô tả ở trên.The default for close_fds was changed from
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
74 to what is described above.

Đã thay đổi trong phiên bản 3.7: Trên Windows, mặc định cho Close_FDS đã được thay đổi từ

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
74 thành
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
27 khi chuyển hướng tay cầm tiêu chuẩn. Bây giờ, nó có thể đặt Close_fds thành
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
27 khi chuyển hướng các tay cầm tiêu chuẩn.On Windows the default for close_fds was changed from
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
74 to
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
27 when redirecting the standard handles. It’s now possible to set close_fds to
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
27 when redirecting the standard handles.

pass_fds là một chuỗi các mô tả tệp tùy chọn để tiếp tục mở giữa cha mẹ và con cái. Cung cấp bất kỳ lực lượng pass_fds gần

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
27. (Chỉ Posix)

Đã thay đổi trong phiên bản 3.2: Tham số pass_fds đã được thêm vào.The pass_fds parameter was added.

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

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, chức năng sẽ thay đổi thư mục làm việc thành CWD trước khi thực hiện trẻ. CWD có thể là một chuỗi, byte hoặc đối tượng giống như đường dẫn. Trên POSIX, hàm tìm kiếm thực thi (hoặc cho mục đầu tiên trong ARGS) so với CWD nếu đường dẫn thực thi là một đường dẫn tương đối.path-like object. On POSIX, the function looks for executable (or for the first item in args) relative to cwd if the executable path is a relative path.

Thay đổi trong phiên bản 3.6: Tham số CWD chấp nhận một đối tượng giống như đường dẫn trên POSIX.cwd parameter accepts a path-like object on POSIX.

Thay đổi trong phiên bản 3.7: Tham số CWD chấp nhận một đối tượng giống như đường dẫn trên Windows.cwd parameter accepts a path-like object on Windows.

Thay đổi trong phiên bản 3.8: Tham số CWD chấp nhận đối tượng byte trên Windows.cwd parameter accepts a bytes object on Windows.

Nếu restore_signals là đúng (mặc định), tất cả các tín hiệu mà python đã đặt thành sig_ign được khôi phục thành sig_dfl trong quy trình con trước khi thực hiện. Hiện tại điều này bao gồm các tín hiệu Sigpipe, SigXFZ và SigXFSZ. (Chỉ Posix)

Đã thay đổi trong phiên bản 3.2: Restore_signals đã được thêm vào.restore_signals was added.

Nếu start_new_session là đúng thì cuộc gọi hệ thống setsid () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. (Chỉ Posix)

Đã thay đổi trong phiên bản 3.2: start_new_session đã được thêm vào.start_new_session was added.

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

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, cuộc gọi hệ thống setregid () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Nếu giá trị được cung cấp là một chuỗi, nó sẽ được tra cứu qua
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
14 và giá trị trong
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15 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: Posix: POSIX

Mới trong phiên bản 3.9.

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

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, cuộc gọi hệ thống setgroup () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Các chuỗi được cung cấp trong các nhóm thêm sẽ được tra cứu qua
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
14 và các giá trị trong
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15 sẽ được sử dụng. Giá trị số nguyên sẽ được truyền nguyên văn. (Chỉ Posix)

Tính khả dụng: Posix: POSIX

Mới trong phiên bản 3.9.

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

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, cuộc gọi hệ thống setgroup () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Các chuỗi được cung cấp trong các nhóm thêm sẽ được tra cứu qua
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
14 và các giá trị trong
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15 sẽ được sử dụng. Giá trị số nguyên sẽ được truyền nguyên văn. (Chỉ Posix)

Tính khả dụng: Posix: POSIX

Mới trong phiên bản 3.9.

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

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, cuộc gọi hệ thống setgroup () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Các chuỗi được cung cấp trong các nhóm thêm sẽ được tra cứu qua
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
14 và các giá trị trong
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15 sẽ được sử dụng. Giá trị số nguyên sẽ được truyền nguyên văn. (Chỉ Posix)

Tính khả dụng: Posix: POSIX

Mới trong phiên bản 3.9.

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

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, cuộc gọi hệ thống setgroup () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Các chuỗi được cung cấp trong các nhóm thêm sẽ được tra cứu qua
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
14 và các giá trị trong
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
15 sẽ được sử dụng. Giá trị số nguyên sẽ được truyền nguyên văn. (Chỉ Posix)

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

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, cuộc gọi hệ thống setreuid () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con. Nếu giá trị được cung cấp là một chuỗi, nó sẽ được tra cứu qua
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
20 và giá trị trong
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
21 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)

Nếu UMASK không âm, cuộc gọi hệ thống Umask () sẽ được thực hiện trong quy trình con trước khi thực hiện quy trình con.must include a valid

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
23.

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

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, thì đó phải là một ánh xạ xác định các biến môi trường cho quy trình mới; Chúng được sử dụng thay vì hành vi mặc định của việc kế thừa môi trường quy trình hiện tại.Frequently Used Arguments. The universal_newlines argument is equivalent to text and is provided for backwards compatibility. By default, file objects are opened in binary mode.

Ghi chúencoding and errors were added.

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

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
23 hợp lệ.text was added as a more readable alias for universal_newlines.

Nếu mã hóa hoặc lỗi được chỉ định hoặc văn bản là đúng, 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 các đối số thường được sử dụng. Đối số Universal_Newlines tương đương với văn bản và được cung cấp cho khả năng tương thích ngược. Theo mặc định, các đối tượng tệp được mở ở chế độ nhị phân.

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    26

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    27

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    28

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    29

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    30

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    31

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    32

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    33

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    34

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    35

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    36

  • Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    37

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 vào dưới dạng bí danh dễ đọc hơn cho Universal_Newlines.The

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
39 parameter was added.

Nếu được đưa ra, startupinfo sẽ là đối tượng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
24, được truyền đến hàm
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
69 bên dưới. Creationflags, nếu được đưa ra, có thể là một hoặc nhiều cờ sau:

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())

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

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
28 đượ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 này). Các nền tảng khác sẽ bỏ qua tham số này.auditing event
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
41 with arguments
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
42,
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
43,
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
44, and
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
45. The value for
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
43 may be a single string or a list of strings, depending on platform.

Mới trong phiên bản 3.10: Tham số

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
39 đã được thêm vào.Added context manager support.

Các đối tượng popen được hỗ trợ dưới dạng các trình quản lý ngữ cảnh thông qua câu lệnh

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
40: Khi thoát, các mô tả tệp tiêu chuẩn được đóng và quá trình được chờ đợi.Popen destructor now emits a
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
47 warning if the child process is still running.

Popen và các chức năng khác trong mô -đun này sử dụng nó nâng cao sự kiện kiểm toán

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
41 với các đối số
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
42,
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
43,
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
44 và
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
45. Giá trị cho
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
43 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.Popen can use
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
48 in some cases for better performance. On Windows Subsystem for Linux and QEMU User Emulation, Popen constructor using
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
48 no longer raise an exception on errors like missing program, but the child process fails with a non-zero
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1.

Đã thay đổi trong phiên bản 3.2: Đã thêm hỗ trợ Trình quản lý bối cảnh.

Đã thay đổi trong phiên bản 3.6: Công cụ phá hủy popen hiện đang phát ra cảnh báo

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
47 nếu quá trình con vẫn đang chạy.

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

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
48 trong một số trường hợp để có hiệu suất tốt hơn. Trên hệ thống con Windows cho mô phỏng người dùng Linux và QEMU, hàm tạo popen sử dụng
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
48 không còn gây ra ngoại lệ về các lỗi như bị thiếu chương trình, nhưng quá trình con không thành công với một
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1 khác không.

Ngoại lệ ha

Các trường hợp ngoại lệ được nêu ra trong quy trình trẻ em, trước khi chương trình mới bắt đầu thực thi, sẽ được nuôi dưỡng lại trong cha mẹ.

Ngoại lệ phổ biến nhất được nâng lên là

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
51. Điều này xảy ra, ví dụ, 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 ngoại lệ
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
51. Lưu ý rằng, khi
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
78,
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
51 sẽ được con nuôi chỉ khi không tìm thấy vỏ được chọn. Để xác định xem shell không tìm thấy ứng dụng được yêu cầu, cần phải kiểm tra mã trả về hoặc đầu ra từ quy trình con.

A

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
55 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!
5 được gọi với các đối số không hợp lệ.

>>> subprocess.run(["ls", "-l"]) # doesn't capture output CompletedProcess(args=['ls', '-l'], returncode=0) >>> subprocess.run("exit 1", shell=True, check=True) Traceback (most recent call last): ... subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1 >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True) CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0, stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'') 11 và >>> subprocess.check_output( ... "ls non_existent_file; exit 0", ... stderr=subprocess.STDOUT, ... shell=True) 'ls: non_existent_file: No such file or directory\n' 3 sẽ tăng with Popen(["ifconfig"], stdout=PIPE) as proc: log.write(proc.stdout.read()) 0 nếu quá trình được gọi là trả lại mã trả lại khác không.

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

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
60 và
Popen(['/bin/sh', '-c', args[0], args[1], ...])
5 sẽ tăng
Popen(['/bin/sh', '-c', args[0], args[1], ...])
6 nếu thời gian chờ hết hạn trước khi quá trình thoát ra.some platforms, it is possible to use
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
65 for this escaping.

Đối tượng 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!
5 có các phương pháp sau:

________ 267 ________ 268 ()()

Kiểm tra nếu quá trình trẻ em đã chấm dứt. Đặt và trả về thuộc tính

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1. Nếu không, trả về
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2.

________ 267 ________ 272 (thời gian chờ = Không) ¶(timeout=None)

Chờ quá trình trẻ em chấm dứt. Đặt và trả về thuộc tính

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1.

Nếu quá trình không chấm dứt sau thời gian chờ giây, hãy tăng ngoại lệ

Popen(['/bin/sh', '-c', args[0], args[1], ...])
6. Nó là an toàn để bắt được ngoại lệ này và thử lại sự chờ đợi.

Ghi chú

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

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

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.timeout was added.

________ 267 ________ 279 (input = none, thời gian chờ = không) ¶(input=None, timeout=None)

Tương tác với quy trình: Gửi dữ liệu đến Stdin. Đọc dữ liệu từ stdout và stderr, cho đến khi đạt được phần cuối. Đợi quá trình chấm dứt và đặt thuộc tính

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1. Đố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
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, nếu không có dữ liệu nào được gửi cho trẻ. Nếu các luồng được mở ở chế độ văn bản, đầu vào phải là một chuỗi. Nếu không, nó phải là byte.

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
82 Trả về một tuple
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
83. Dữ liệu sẽ là chuỗi nếu các luồng được mở ở chế độ văn bản; Nếu không, byte.

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

Popen(['/bin/sh', '-c', args[0], args[1], ...])
9. Tương tự, để có được bất cứ thứ gì khác ngoài
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2 trong kết quả, bạn cũng cần cung cấp cho
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 và/hoặc
Popen(['/bin/sh', '-c', args[0], args[1], ...])
2.

Nếu quá trình không chấm dứt sau khi hết thời gian, một ngoại lệ

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

Quá trình trẻ em không bị giết nếu hết thời gian chờ, vì vậy để dọn dẹp đúng một ứng dụng được cư xử tốt nên tiêu diệt quy trình trẻ em và kết thúc giao tiếp:

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()

Ghi chú

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

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

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.timeout was added.

________ 267 ________ 279 (input = none, thời gian chờ = không) ¶(signal)

Tương tác với quy trình: Gửi dữ liệu đến Stdin. Đọc dữ liệu từ stdout và stderr, cho đến khi đạt được phần cuối. Đợi quá trình chấm dứt và đặt thuộc tính

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1. Đố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
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, nếu không có dữ liệu nào được gửi cho trẻ. Nếu các luồng được mở ở chế độ văn bản, đầu vào phải là một chuỗi. Nếu không, nó phải là byte.

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
82 Trả về một tuple
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
83. Dữ liệu sẽ là chuỗi nếu các luồng được mở ở chế độ văn bản; Nếu không, byte.

Ghi chú

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

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

Thay đổi trong phiên bản 3.3: Thời gian chờ đã được thêm vào.()

________ 267 ________ 279 (input = none, thời gian chờ = không) ¶

Tương tác với quy trình: Gửi dữ liệu đến Stdin. Đọc dữ liệu từ stdout và stderr, cho đến khi đạt được phần cuối. Đợi quá trình chấm dứt và đặt thuộc tính
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1. Đố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
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2, nếu không có dữ liệu nào được gửi cho trẻ. Nếu các luồng được mở ở chế độ văn bản, đầu vào phải là một chuỗi. Nếu không, nó phải là byte.()

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
82 Trả về một tuple
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
83. Dữ liệu sẽ là chuỗi nếu các luồng được mở ở chế độ văn bản; Nếu không, byte.

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

Popen(['/bin/sh', '-c', args[0], args[1], ...])
9. Tương tự, để có được bất cứ thứ gì khác ngoài
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
2 trong kết quả, bạn cũng cần cung cấp cho
Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 và/hoặc
Popen(['/bin/sh', '-c', args[0], args[1], ...])
2.

Nếu quá trình không chấm dứt sau khi hết thời gian, một ngoại lệ
Popen(['/bin/sh', '-c', args[0], args[1], ...])
6 sẽ được nâng lên. Bắt được ngoại lệ này và giao tiếp thử lại sẽ không mất bất kỳ đầu ra nào.

Quá trình trẻ em không bị giết nếu hết thời gian chờ, vì vậy để dọn dẹp đúng một ứng dụng được cư xử tốt nên tiêu diệt quy trình trẻ em và kết thúc giao tiếp:

Dữ liệu đọc được đệm trong bộ nhớ, 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.

________ 267 ________ 290 (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.

Trên Windows, Sigterm là bí danh cho

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
91. CTRL_C_EVENT và CTRL_BREAK_EVENT có thể được gửi đến các quy trình bắt đầu với tham số CreationFlags bao gồm created_new_process_group.

________ 267 ________ 293 ()

Ngừng đứa trẻ. Trên Posix OSS, phương pháp gửi Sigterm cho đứa trẻ. Trên Windows, hàm API Win32

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
94 được gọi để ngăn chặn đứa trẻ.

________ 267 ________ 296 ()

Giết chết đứa trẻ. Trên posix oss, chức năng gửi sigkill cho trẻ. Trên Windows

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
97 là bí danh cho
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
91.

Các thuộc tính sau cũng có sẵn:

________ 267 ________ 57¶

Đối số Args khi nó được chuyển đế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!
5 - một chuỗi các đối số chương trình hoặc một chuỗi khác.

Mới trong phiên bản 3.3.

Windows Popen Helpers¶

Lớp

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
24 và các hằng số sau chỉ có sẵn trên Windows.

Lớp ________ 36 ________ 336 (*, dwflags = 0, hstdinput = none, hstDoutput = none, hstderror = none, wshowWindow = 0, lpAttributelist = none) ¶(*, dwFlags=0, hStdInput=None, hStdOutput=None, hStdError=None, wShowWindow=0, lpAttributeList=None)

Hỗ trợ một phần của 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!
5. Các thuộc tính sau đây có thể được đặt bằng cách truyề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.Keyword-only argument support was added.

________ 338¶

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

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
24 có được sử dụng khi quá trình tạo ra một cửa sổ.

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW

________ 340¶

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!
41 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!
42, thuộc tính này là tay cầm đầ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!
42 không được chỉ định, mặc định cho đầu vào tiêu chuẩn là bộ đệm bàn phím.

________ 344¶

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!
41 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!
42, thuộc tính này là tay cầm đầ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 sổ console.

________ 347¶

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!
41 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!
42, 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 sổ console.

________ 350¶

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!
41 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!
52, 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!
53 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!
54. Nếu không, 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!
55 đượ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!
5 được gọi với
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
78.

________ 358¶

Một từ điển các thuộc tính bổ sung để tạo quá 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!
59, xem UpdateProcThreadAttribution.

Các thuộc tính được hỗ trợ:

handle_list

Trình tự của tay cầm 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ằ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!
60 khi được chuyển cho 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!
5, nếu không
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
51 sẽ được nâng lê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!
63 (87).

Cảnh báo

Trong một quy trình đa luồng, sử dụng thận trọng để tránh các tay cầm bị rò rỉ có thể di truyền khi kết hợp tính năng này với các cuộc 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 tay cầm 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!
64. Đ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 tay cầm có thể thừa kế.

Mới trong phiên bản 3.7.

Hằng số cửa sổ hằng số

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!
1 hiển thị các hằng số sau.

________ 36 ________ 367¶

Thiết bị đầu vào tiêu chuẩn. Ban đầu, đây là bộ đệm đầu vào 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!
68.

________ 36 ________ 370¶

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 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!
71.

________ 36 ________ 373¶

Thiết bị lỗi tiêu chuẩn. Ban đầu, đây là bộ đệm màn hình bảng điều khiển 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!
71.

________ 36 ________ 376¶

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

________ 36 ________ 378¶

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!
79,
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> 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 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!
81 chứa thông tin bổ sung.

________ 36 ________ 383¶

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!
84 chứa thông tin bổ sung.

________ 36 ________ 386¶

Quá trình mới có một giao diện điều khiển mới, thay vì kế thừa bảng điều khiển cha mẹ của nó (mặc định).

________ 36 ________ 388¶

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!
5
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> 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 để 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!
91 trên quy trình con.

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

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
26 được chỉ định.

________ 36 ________ 394¶

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!
5
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> 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 để chỉ định rằng một quy trình mới sẽ có mức độ ưu tiên trên trung bình.

Mới trong phiên bản 3.7.

Hằng số cửa sổ hằng số

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!
1 hiển thị các hằng số sau.

Mới trong phiên bản 3.7.

Hằng số cửa sổ hằng số

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!
1 hiển thị các hằng số sau.

Mới trong phiên bản 3.7.

Hằng số cửa sổ hằng số

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!
1 hiển thị các hằng số sau.

Mới trong phiên bản 3.7.

Hằng số cửa sổ hằng số

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!
1 hiển thị các hằng số sau.

Mới trong phiên bản 3.7.

Hằng số cửa sổ hằng số

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!
1 hiển thị các hằng số sau.

Mới trong phiên bản 3.7.

________ 36 ________ 418¶

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!
5
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> 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 để chỉ định rằng một quy trình mới sẽ không tạo cửa sổ.

Mới trong phiên bản 3.7.

________ 36 ________ 422¶

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!
5
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> 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 để chỉ định rằng một quy trình mới sẽ không kế thừa bảng điều khiển cha mẹ của nó. Giá trị này không thể được sử dụng với create_new_console.

Mới trong phiên bản 3.7.

________ 36 ________ 422¶

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!
5
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> 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 để chỉ định rằng một quy trình mới sẽ không kế thừa bảng điều khiển cha mẹ của nó. Giá trị này không thể được sử dụng với create_new_console.

Mới trong phiên bản 3.7.

________ 36 ________ 422¶

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!
5
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> 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 để chỉ định rằng một quy trình mới sẽ không kế thừa bảng điều khiển cha mẹ của nó. Giá trị này không thể được sử dụng với create_new_console.

Mới trong phiên bản 3.7.

________ 36 ________ 422¶

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!
5
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> 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 để chỉ định rằng một quy trình mới sẽ không kế thừa bảng điều khiển cha mẹ của nó. Giá trị này không thể được sử dụng với create_new_console.

________ 36 ________ 426¶(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

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!
5
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> 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 để chỉ định rằng một quy trình mới không kế thừa chế độ lỗi của quá trình gọi. Thay vào đó, quy trình mới có chế độ lỗi mặc định. Tính năng này đặc biệt hữu ích cho các ứng dụng shell đa luồng chạy với các lỗi cứng bị vô hiệu hóa.

________ 36 ________ 430¶

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!
5
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> 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 để chỉ định rằng một quy trình mới không liên quan đến công việc.

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ý phụ. 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!
4 trong nhiều trường hợp, nhưng rất nhiều mã hiện tại gọi các chức năng này.

________ 36 ________ 435 (args, *, stdin = none, stdout = none, stderr = none, shell = false

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

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1.timeout was added.

Mã cần bắt giữ Stdout hoặc Stderr 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!
4 thay thế:(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, **other_popen_kwargs)

Để ngăn chặn stdout hoặc stderr, cung cấp giá trị

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
29.

________ 36 ________ 430¶

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!
5
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> 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 để chỉ định rằng một quy trình mới không liên quan đến công việc.

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ý phụ. 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!
4 trong nhiều trường hợp, nhưng rất nhiều mã hiện tại gọi các chức năng này.

________ 36 ________ 435 (args, *, stdin = none, stdout = none, stderr = none, shell = false

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

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1.timeout was added.

Mã cần bắt giữ Stdout hoặc Stderr 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!
4 thay thế:(args, *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None, errors=None, universal_newlines=None, timeout=None, text=None, **other_popen_kwargs)

Để ngăn chặn stdout hoặc stderr, cung cấp giá trị

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
29.

Các đối số hiển thị ở trên chỉ là một số thông thường. Chữ ký chức năng đầy đủ giống như hàm xây 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!
5 - hàm này chuyển tất cả các đối số được cung cấp khác ngoài thời gian chờ trực tiếp đến giao diện đó.

Ghi chú

run(..., check=True, stdout=PIPE).stdout

Không sử dụng

Popen(['/bin/sh', '-c', args[0], args[1], ...])
1 hoặc
Popen(['/bin/sh', '-c', args[0], args[1], ...])
2 với chức năng này. Quá trình con sẽ chặn nếu nó tạo ra đủ đầu ra cho một đường ống để lấp đầy bộ đệm ống OS 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 vào.

________ 36 ________ 443 (args, *, stdin = none, stdout = none, stderr = none, shell = falseFrequently Used Arguments 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!
4.

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

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
0. Đối tượng
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
0 sẽ có mã trả về trong thuộc tính
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1. Nếu
>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
11 không thể bắt đầu quá trình, nó sẽ tuyên truyền ngoại lệ đã được nêu ra.

>>> subprocess.check_output(
...     "ls non_existent_file; exit 0",
...     stderr=subprocess.STDOUT,
...     shell=True)
'ls: non_existent_file: No such file or directory\n'

________ 36 ________ 454 (args, *, stdin = none, stderr = none, shell = false, cwd = none

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

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1.timeout was added.

Mã cần bắt giữ Stdout hoặc Stderr 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!
4 thay thế:Support for the input keyword argument was added.

Để ngăn chặn stdout hoặc stderr, cung cấp giá trị

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
29.encoding and errors were added. See
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
4 for details.

Các đối số hiển thị ở trên chỉ là một số thông thường. Chữ ký chức năng đầy đủ giống như hàm xây 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!
5 - hàm này chuyển tất cả các đối số được cung cấp khác ngoài thời gian chờ trực tiếp đến giao diện đó.text was added as a more readable alias for universal_newlines.

Ghi chú

Không sử dụng

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

Ghi chú

Tất cả các chức năng của một người khác trong phần này đều không thành công (ít nhiều) nếu không thể tìm thấy chương trình thực hiện; Thay vào đó, các thay thế của B Biêu tăng

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
51 thay thế.

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

>>> subprocess.check_output(
...     "ls non_existent_file; exit 0",
...     stderr=subprocess.STDOUT,
...     shell=True)
'ls: non_existent_file: No such file or directory\n'
3 sẽ thất bại với
with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
0 nếu hoạt động được yêu cầu tạo ra mã trả lại khác không. Đầu ra vẫn có sẵn dưới dạng thuộc tính
Popen(['/bin/sh', '-c', args[0], args[1], ...])
58 của ngoại lệ được nâng lên.

Trong các ví dụ sau, chúng tôi giả định 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!
1.

Thay thế /Bin /SH Shell Lệnh thay thế SHELL/bin/sh shell command substitution¶

becomes:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
0

Thay thế đường ống shell

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
1

becomes:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
2

Cuộc gọi

Popen(['/bin/sh', '-c', args[0], args[1], ...])
74 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 Shell Shell vẫn có thể được sử dụng trực tiếp:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
1

becomes:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
4

Thay thế ________ 364¶

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
5

Notes:

  • Gọi chương trình thông qua vỏ thường không được yêu cầu.

  • Giá trị trả về

    Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
    
    60 được mã hóa khác với 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!
    
    64.

  • 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!
    
    64 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 làm điều 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!
    
    1.

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

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
6

Thay thế cho gia đình Popen(['/bin/sh', '-c', args[0], args[1], ...]) 80

Ví dụ P_Nowait:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
7

Ví dụ P_Wait:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
8

Ví dụ về vector:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
9

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

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
0

Thay thế Popen(['/bin/sh', '-c', args[0], args[1], ...]) 81, Popen(['/bin/sh', '-c', args[0], args[1], ...]) 82, ________ 483¶

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
1

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
2

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
3

Xử lý mã trả lại dịch như sau:

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
4

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

Ghi chú

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 đó là một danh sách, lệnh được thực thi trực tiếp.

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
5

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
6

Popen(['/bin/sh', '-c', args[0], args[1], ...])
85 và
Popen(['/bin/sh', '-c', args[0], args[1], ...])
86 về cơ bản hoạt động như
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
41, 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!
    
    5 làm tăng một ngoại lệ nếu việc thực hiện thất bại.

  • Đối số Capturestderr được thay thế bằng đối số Stderr.

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

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

    Popen(['/bin/sh', '-c', args[0], args[1], ...])
    
    91 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!
    
    5 để đảm bảo hành vi này trên tất cả các nền tảng hoặc các phiên bản Python trong quá khứ.

Chức năng gọi vỏ di sản Legacy

Mô -đun này cũng cung cấp các hàm di sản sau đây từ mô -đun 2.x

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

________ 36 ________ 495 (CMD) ¶(cmd)

Trả về

Popen(['/bin/sh', '-c', args[0], args[1], ...])
96 của việc thực thi CMD trong vỏ.

Thực hiện chuỗi CMD trong shell với

Popen(['/bin/sh', '-c', args[0], args[1], ...])
97 và trả về 2-Tuple
Popen(['/bin/sh', '-c', args[0], args[1], ...])
96. Mã hóa địa phương được sử dụng; Xem các ghi chú về các đối số thường được sử dụng để biết thêm chi tiết.Frequently Used Arguments for more details.

Một dòng mới được truyền ra từ đầu ra. Mã thoát cho lệnh có thể được hiểu là mã trả lại của quy trình con. Thí dụ:

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
7

Tính khả dụng: POSIX & Windows.: POSIX & Windows.

Thay đổi trong phiên bản 3.3.4: Hỗ trợ Windows đã được thêm vào.Windows support was added.

Hàm bây giờ trả về (outcode, đầu ra) thay vì (trạng thái, đầu ra) như trong Python 3.3.3 và sớm hơn. EXITCODE có cùng giá trị với

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
1.

________ 36 ________ 501 (CMD) ¶(cmd)

Trả về đầu ra (stdout và stderr) của việc thực thi cmd trong shell.

Giống như

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())
02, 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ụ:

Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
8

Tính khả dụng: POSIX & Windows.: POSIX & Windows.

Thay đổi trong phiên bản 3.3.4: Hỗ trợ Windows đã được thêm vào.Windows support added

Hàm bây giờ trả về (outcode, đầu ra) thay vì (trạng thái, đầu ra) như trong Python 3.3.3 và sớm hơn. EXITCODE có cùng giá trị với si = subprocess.STARTUPINFO() si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW 1.

________ 36 ________ 501 (CMD) ¶

Trả về đầu ra (stdout và stderr) của việc thực thi cmd trong shell.

  1. Giống như

    with Popen(["ifconfig"], stdout=PIPE) as proc:
        log.write(proc.stdout.read())
    
    02, 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ụ:

  2. Đã thay đổi trong phiên bản 3.3.4: được thêm hỗ trợ Windows

  3. Notes¶

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

  5. 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):

Các đối số được phân định bởi không gian trắng, đó là không gian hoặc một tab.

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

Một dấu ngoặc kép có trước một dấu gạch chéo ngược được hiểu là một dấu ngoặc kép theo nghĩa đen.

Làm cách nào để chạy lệnh Python bằng cách sử dụng quy trình con trong Linux?

Chạy một lệnh với hệ thống phụ () cho các trường hợp sử dụng cơ bản. Trong dòng đầu tiên, chúng tôi nhập mô -đun phụ, là một phần của thư viện tiêu chuẩn Python. Sau đó, chúng tôi sử dụng hàm SubProcess.run () để thực thi lệnh.use the subprocess. run() function to execute the command.

Làm cách nào để chạy một thực thi từ Python?

Các bước để tạo một thực thi bằng pyinstaller..
Bước 1: Thêm Python vào đường dẫn Windows. Để bắt đầu, bạn có thể muốn thêm Python vào đường dẫn Windows. ....
Bước 2: Cài đặt gói pyinstaller. ....
Bước 3: Lưu tập lệnh Python của bạn. ....
Bước 4: Tạo thực thi bằng PyInstaller. ....
Bước 5: Chạy thực thi ..

Làm cách nào để chạy lệnh shell trong Python bằng cách sử dụng quy trình con?

Chức năng Chức năng Run Run () của Python đã được thêm vào Python 3.5 và nên sử dụng hàm Run () để thực thi các lệnh shell trong chương trình Python.Đối số ARGS trong quy trình con.Chức năng chạy () lấy lệnh shell và trả về một đối tượng hoàn thành trong Python.use the run() function to execute the shell commands in the python program. The args argument in the subprocess. run() function takes the shell command and returns an object of CompletedProcess in Python.

Làm cách nào để chạy một quy trình con trong Python?

Để bắt đầu một quy trình mới, hay nói cách khác, một quy trình con mới trong Python, bạn cần sử dụng cuộc gọi chức năng popen.Có thể vượt qua hai tham số trong cuộc gọi chức năng.Tham số đầu tiên là chương trình bạn muốn bắt đầu và thứ hai là đối số tệp.use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.