I have a command that looks like this:
ps v -p 2585 | awk '{if ($9 != "%MEM") {print $9}}'
Now this runs fine in bash, it just takes the memory portion of whatever pid you give it. However I am now trying to implement it in python but I am having some issues. Here is what I have in python
cmd1 = ['ps', 'v', '-p', pid]
cmd2 = ['awk', '\'{if ($9 != "%MEM") {print $9}}\'']
Now to run them...
runcmd1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE)
runcmd2 = subprocess.Popen(cmd2, stdin=runcmd1.stdout, stdout=subprocess.PIPE)
I get this error:
awk: '{if (\$9 != "%MEM") {print \$9}}'
awk: ^ invalid char ''' in expression
I used this to print out what the command looked like together... sys.stdout.write(' '.join(cmd1)+' '+'|'+' '+' '.join(cmd2)+'\n')
And it gave:
ps v -p 1073 | awk '{if ($9 != "%MEM") {print $9}}'
I see no difference between this and the actual bash command that works. Can anyone help?
You don't need to protect the awk commands from the shell when you're running it via popen (the arguments are already split into a list, so your whitespace is left alone).
cmd2 = ['awk', '{if ($9 != "%MEM") {print $9}}']
will work fine.
Python has some nice ways of writing strings that avoid escaping like you tried to do here, for situations where you do need it:
'''In this string, I don't need to escape a single ' character,
or even a new-line, because the string only ends
when it gets three ' characters in a row like this:'''
"""The same is true of double-quotes like this.
Of course, whitespace and both the ' and " quote characters
are safe in here."""
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