我在正确转义子进程调用时遇到问题
我想调用 sed -n "$="/path/to/file
来计算文件中的行数,但无法从 python 中调用。我的代码如下:
import subprocess
filename = "/path/to/file"
cmd = subprocess.Popen(["sed", "-n '$='", filename], stdout=subprocess.PIPE)
cmd_out, cmd_err = cmd.communicate()
num_lines = int(cmd_out.strip())
print num_lines
我已经为“-n '$='”尝试了不同的转义组合,但似乎没有任何效果。
最佳答案
-n
和 $=
是两个独立的参数。
["sed", "-n", "$=", filename]
关于python - 在 python 中转义子进程调用的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17836931/