Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to escape a subprocess call in python

Tags:

python

sed

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.

like image 209
markz Avatar asked Jul 24 '13 14:07

markz


People also ask

How do you end a subprocess call in Python?

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.

How do I terminate subprocess?

To terminate the subprocess, call subprocess. Popen. terminate() with subprocess. Popen as the result from the previous step.

Does subprocess call wait for completion?

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.

How can we avoid shell true in subprocess?

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).


2 Answers

-n and $= are two separate arguments.

["sed", "-n", "$=", filename]
like image 59
Ignacio Vazquez-Abrams Avatar answered Sep 30 '22 10:09

Ignacio Vazquez-Abrams


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)
like image 25
Hemanth Avatar answered Sep 30 '22 10:09

Hemanth