Hướng dẫn pass variable to subprocess.popen python - chuyển biến tới subprocess.popen python

Khi bạn gọi subprocess.Popen, bạn có thể chuyển một chuỗi hoặc danh sách cho lệnh được chạy. Nếu bạn vượt qua một danh sách, các mục sẽ được chia theo một cách cụ thể.

Trong trường hợp của bạn, bạn cần chia nó một cái gì đó như thế này:

command = ["python",  "mytool.py", "-a", servers[server]['address'], 
           "-x", servers[server]['port'], 
           "-p", servers[server]['pass'], 
           "some",  "additional", "command"]
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

Điều này là do nếu bạn chuyển trong danh sách, Popen giả định rằng bạn đã chia dòng lệnh thành các từ (các giá trị sẽ kết thúc trong

VARIABLE=12
v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v VARIABLE", shell=True);
0), vì vậy nó không cần phải.

Cách bạn gọi nó, nó sẽ cố gắng chạy một nhị phân gọi là "Python mytool.py -a", đó không phải là ý của bạn.

Một cách khác để khắc phục nó là tham gia tất cả các từ vào một chuỗi (mà Popen sau đó sẽ tách ra - xem

VARIABLE=12
v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v VARIABLE", shell=True);
2). Nhưng tốt hơn hết là bạn nên sử dụng phiên bản danh sách nếu có thể - nó mang lại sự kiểm soát đơn giản hơn về cách phân chia dòng lệnh (nếu các đối số có khoảng trắng hoặc trích dẫn trong đó) mà không phải lộn xộn với việc trích dẫn các ký tự trích dẫn.

Chào mọi người,

Tôi muốn chuyển một biến cho lệnh này:

VARIABLE=12
v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v VARIABLE", shell=True);

Làm thế nào tôi có thể làm điều đó? Tôi đã thử:
I tried:

VARIABLE=12
v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v %s",VARIABLE, shell=True);

Nhưng không hoạt động.

Bài viết: 6.4286,428

Chủ đề: 115115

Tham gia: Tháng 9 năm 2016Sep 2016

Danh tiếng: 483 483

Định dạng chuỗi và bây giờ sử dụng

VARIABLE=12
v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v VARIABLE", shell=True);
3 Mới trong Python 3.6 ➡

>>> VARIABLE = 12
>>> s = f"cat /tmp/logall | sort -u | grep -v {VARIABLE}"
>>> s
'cat /tmp/logall | sort -u | grep -v 12'

Bài viết: 2828

Chủ đề: 1616

Tham gia: Mar 2022Mar 2022

Danh tiếng: 0 0

.snippsat Wrote: String formatting and now use
VARIABLE=12
v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v VARIABLE", shell=True);
3 new in Python 3.6 ➡
>>> VARIABLE = 12
>>> s = f"cat /tmp/logall | sort -u | grep -v {VARIABLE}"
>>> s
'cat /tmp/logall | sort -u | grep -v 12'

Có cách nào để trong Python 2.7 không? Tôi chưa thể sử dụng 3.

Bài viết: 2828

Chủ đề: 1616

Tham gia: Mar 2022Mar 2022

Danh tiếng: 0 0

.paulo79 Wrote:
.snippsat Wrote: String formatting and now use
VARIABLE=12
v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v VARIABLE", shell=True);
3 new in Python 3.6 ➡
>>> VARIABLE = 12
>>> s = f"cat /tmp/logall | sort -u | grep -v {VARIABLE}"
>>> s
'cat /tmp/logall | sort -u | grep -v 12'

Có cách nào để trong Python 2.7 không? Tôi chưa thể sử dụng 3.

(Tháng 4 đến 12-2022, 11:32 sáng) Paulo79 đã viết:

>>> VARIABLE=12
>>> v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v {}".format(VARIABLE), shell=True);
>>>
>>> print(v_num_final_distinct_grid_disks_per_cell_and_dg)
10

>>> VARIABLE=10
>>> v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v {}".format(VARIABLE), shell=True);
>>> print(v_num_final_distinct_grid_disks_per_cell_and_dg)
12

>>>

Có cách nào để trong Python 2.7 không? Tôi chưa thể sử dụng 3.1,868

Tôi đã tìm thấy nó:8

Bài viết: 1.868May 2017

Chủ đề: 8 211

Tham gia: Tháng 5 năm 2017 (This post was last modified: Apr-12-2022, 12:35 PM by DeaD_EyE.)

Danh tiếng: 211

import subprocess

# VARIABLE=12
# v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v VARIABLE", shell=True)
# grep -v is invert match


def insecure(variable: str) -> str:
    return subprocess.check_output(f"cat /tmp/logall | sort -u | grep -v {variable}", shell=True, encoding="utf8")

    
def silly(variable: str) -> str:
    """
    Using PIPE to connect stdout with stdin of next process
    """
    cat_proc = subprocess.Popen(["cat", "/tmp/logall"], encoding="utf8", stdout=subprocess.PIPE)
    sort_proc = subprocess.Popen(["sort", "-u"], encoding="utf8", stdin=cat_proc.stdout, stdout=subprocess.PIPE)
    grep_proc = subprocess.Popen(["grep", "-v", variable], encoding="utf8", stdin=sort_proc.stdout, stdout=subprocess.PIPE)
    grep_proc.wait()
    return grep_proc.stdout.read()


def better(variable: str) -> str:
    lines = []
    with open("/tmp/logall") as fd:
        # fd is the the file-object
        # iterating over fd -> lines
        # creating a set from fd -> unique lines
        # sort set -> sorted unique lines
        for line in sorted(set(fd)):
            if variable not in line:
                lines.append(line.rstrip())
    return "\n".join(lines)


def better2(variable: str) -> list[str]:
    with open("/tmp/logall") as fd:
        return [
            line.rstrip()
            for line in sorted(set(fd))
            if variable not in line
        ]

Tháng 4 đến 12-2022, 12:35 PM (Bài đăng này đã được sửa đổi lần cuối: Tháng 4-12-2022, 12:35 PM bởi Dead_eye.)not matching lines as a list. The other functions do return the whole text as a single

VARIABLE=12
v_num_final_distinct_grid_disks_per_cell_and_dg = subprocess.check_output("cat /tmp/logall | sort -u | grep -v VARIABLE", shell=True);
7.

4 lựa chọn thay thế:
All humans together. We don't need politicians!

Sử dụng phương thức popen Phương thức popen không chờ đợi để hoàn thành quy trình để trả về giá trị.

Làm thế nào để bạn chuyển một chuỗi làm đầu vào cho một quy trình con trong Python ?..
args = ["grep", "đậu"].
Child_Proccess = Sub thế giới. Popen (args, stdin = quy trình con ..
trẻ con. Stdin ..
Child_Process_Output = Child_Proccess. giao tiếp () [0].
trẻ con. Stdin ..
print(child_process_output).

Làm cách nào để sử dụng popen phụ trong Python?

Cách tiếp cận được đề xuất để gọi các quy trình con là sử dụng hàm chạy () cho tất cả các trường hợp sử dụng mà nó có thể xử lý.Đối với các trường hợp sử dụng nâng cao hơn, giao diện popen cơ bản có thể được sử dụng trực tiếp.Chạy lệnh được mô tả bởi Args.Đợi lệnh hoàn thành, sau đó trả lại một thể hiện hoàn thành.use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly. Run the command described by args. Wait for command to complete, then return a CompletedProcess instance.

Sự khác biệt giữa quá trình điều hành phụ và popen phụ là gì?

Sự khác biệt chính là quá trình phụ.chạy () thực thi một lệnh và chờ nó kết thúc, trong khi với quy trình con.Popen Bạn có thể tiếp tục làm công cụ của mình trong khi quá trình kết thúc và sau đó chỉ gọi Popen.Giao tiếp () bản thân để truyền và nhận dữ liệu cho quy trình của bạn.

Popen phụ có chờ đợi để hoàn thành không?

Sử dụng phương thức popen Phương thức popen không chờ đợi để hoàn thành quy trình để trả về giá trị.does not wait to complete the process to return a value.