subprocess + grep 用于反斜杠终止的字符串
subprocess + grep for backslash terminated string
我想使用子进程来 grep 以反斜杠结尾的字符串:
product = "myProduct\"
所以在 bash 中,如果我这样做:
zgrep -n 'myProduct\' file_of_products.txt.gz
它有效(我得到了一堆线条)。现在在 python:
中尝试
import subprocess
product = "myProduct\"
command = f"""zgrep -n "{product}" {filename}"""
return_code, output = subprocess.getstatusoutput(command)
我明白了;
'/bin/sh: 1: Syntax error: Unterminated quoted string'
您可以将此重构代码与原始字符串和单引号一起使用:
import subprocess
product = r"myProduct\"
command = f"zgrep -n '{product}' '{filename}'"
return_code, output = subprocess.getstatusoutput(command)
我想使用子进程来 grep 以反斜杠结尾的字符串:
product = "myProduct\"
所以在 bash 中,如果我这样做:
zgrep -n 'myProduct\' file_of_products.txt.gz
它有效(我得到了一堆线条)。现在在 python:
中尝试import subprocess
product = "myProduct\"
command = f"""zgrep -n "{product}" {filename}"""
return_code, output = subprocess.getstatusoutput(command)
我明白了;
'/bin/sh: 1: Syntax error: Unterminated quoted string'
您可以将此重构代码与原始字符串和单引号一起使用:
import subprocess
product = r"myProduct\"
command = f"zgrep -n '{product}' '{filename}'"
return_code, output = subprocess.getstatusoutput(command)