Print file contents using cat in subprocess
<p>I am using a subprocess call in python where I have to print a file contents using <code>cat</code>. The file name is a variable that I generate in the python code itself. This is my code:</p>

<pre><code>pid = str(os.getpid())
tmp_file_path = "/tmp/" + pid + "/data_to_synnet"
synnet_output = subprocess.check_output(["cat echo '%s'"%tmp_file_path], shell=True)
</code></pre>

<p>The above code throws an error saying <code>cat: echo: No such file or directory</code>.</p>

<p>However, when I use only <code>subprocess.check_output(["echo '%s'"%tmp_file_path], shell=True)</code>, the variable name is printed correctly.</p>

<p>Also, I tried doing this (<code>cat echo $tmp_file_name</code>) in the command line and it works. Can someone please tell what is wrong?</p>

<p>The command you want is this:</p>

<pre><code>"cat '%s'"%tmp_file_path
</code></pre>

<p>Just get rid of the "echo" word.</p>

<p>Alternatively,</p>

<pre><code> synnet_output = subprocess.check_output(["cat", tmp_file_path], shell=False)
</code></pre>

python shell subprocess