I am having problems correctly escaping a subprocess call
I want to call sed -n "$=" /path/to/file
to count the number of lines in a file but fail to do so from python. My code is as follows:
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
I have tried different escaping combinations for "-n '$='" but nothing seems to work.
Popen(args) with args as a sequence of program arguments or a single string to execute a child program in a new process with the supplied arguments. To terminate the subprocess, call subprocess. Popen. terminate() with subprocess.
To terminate the subprocess, call subprocess. Popen. terminate() with subprocess. Popen as the result from the previous step.
The subprocess module provides a function named call. This function allows you to call another program, wait for the command to complete and then return the return code.
From the docs: args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names).
-n
and $=
are two separate arguments.
["sed", "-n", "$=", filename]
By default, subprocess.Popen has shell=False.
Without invoking the shell, each argument is passed uninterpreted to sed So sed reads arguments as -n '$='
When you run the command sed -n '$=' , shell removes the '' before sending to the command sed.
So your $= should be without quotes
And as specified by Ignacio, -n $= arguments should be separate
cmd = subprocess.Popen(["sed", "-n", "$=", filename], stdout=subprocess.PIPE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With