unable to use cmd command in python
<p>I am using this command to find disk usage in my shell script</p>

<pre><code>df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'
</code></pre>

<p>I want to use this command in my python script</p>

<pre><code>ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'")
</code></pre>

<p>but unable to handle string literals.</p>

<p>I tried replacing \ with \\ as suggested <a href="http://stackoverflow.com/questions/2102452/why-this-dos-command-does-not-work-inside-python">here</a> </p>

<p>Can someone throw light on thiS?</p>

<p>This is the error i am getting </p>

<pre><code>ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'")
                                                                                           ^
SyntaxError: invalid syntax
</code></pre>

<p>You can try the following using <a href="https://docs.python.org/2/library/shlex.html" rel="nofollow">shlex</a> and <a href="https://docs.python.org/2/library/subprocess.html" rel="nofollow">subprocess</a> to prepare your command string.</p>

<pre><code>from subprocess import list2cmdline
import shlex

cmd = list2cmdline(shlex.split("""df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'"""))

ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd)
</code></pre>

python shell