如何使用子流程剪切变量?
How can I cut a variable with subprocces?
out=subprocess.check_output("""cat /etc/passwd""", universal_newlines=True, shell=True)
for line in out.splitlines():
elem=subprocess.check_output(["echo", "line", "|", "cut", "-d:", "-f1"],
universal_newlines=True, shell=True)
lboxS.insert(END,elem)
elem=subprocess.check_output(["echo", "line", "|", "cut", "-d:", "-f1"], universal_newlines=True, shell=True)
如果您使用 shell=True
,您必须为 args 传递一个字符串(就像您对 out
所做的那样):
elem = subprocess.check_output("echo '"+line+"'|cut -d: -f1", text=True, shell=True).rstrip()
或
elem = subprocess.check_output("echo '%s'|cut -d: -f1"%line, text=True, shell=True).rstrip()
或
elem = subprocess.check_output(f"echo '{line}'|cut -d: -f1", text=True, shell=True).rstrip()
out=subprocess.check_output("""cat /etc/passwd""", universal_newlines=True, shell=True)
for line in out.splitlines():
elem=subprocess.check_output(["echo", "line", "|", "cut", "-d:", "-f1"],
universal_newlines=True, shell=True)
lboxS.insert(END,elem)
elem=subprocess.check_output(["echo", "line", "|", "cut", "-d:", "-f1"], universal_newlines=True, shell=True)
如果您使用 shell=True
,您必须为 args 传递一个字符串(就像您对 out
所做的那样):
elem = subprocess.check_output("echo '"+line+"'|cut -d: -f1", text=True, shell=True).rstrip()
或
elem = subprocess.check_output("echo '%s'|cut -d: -f1"%line, text=True, shell=True).rstrip()
或
elem = subprocess.check_output(f"echo '{line}'|cut -d: -f1", text=True, shell=True).rstrip()