<p>I would like to replicate this in python:</p>

<pre><code>gvimdiff &lt;(hg cat file.txt) file.txt
</code></pre>

<p>(hg cat file.txt outputs the most recently committed version of file.txt)</p>

<p>I know how to pipe the file to gvimdiff, but it won't accept another file:</p>

<pre><code>$ hg cat file.txt | gvimdiff file.txt -
Too many edit arguments: "-"
</code></pre>

<p>Getting to the python part...</p>

<pre><code># hgdiff.py
import subprocess
import sys
file = sys.argv[1]
subprocess.call(["gvimdiff", "&lt;(hg cat %s)" % file, file])
</code></pre>

<p>When subprocess is called it merely passes <code>&lt;(hg cat file)</code> onto <code>gvimdiff</code> as a filename.</p>

<p>So, is there any way to redirect a command as bash does?
For simplicity's sake just cat a file and redirect it to diff:</p>

<pre><code>diff &lt;(cat file.txt) file.txt
</code></pre>

[{'answer_id': 78459, 'author': 'Mark Hattarki', 'author_id': 14424, 'author_profile': 'https://Stackoverflow.com/users/14424', 'pm_score': 2, 'selected': False, 'text': '<p>There is also the commands module:</p>\n\n<pre><code>import commands\n\nstatus, output = commands.getstatusoutput("gvimdiff &lt;(hg cat file.txt) file.txt")\n</code></pre>\n\n<p>There is also the popen set of functions, if you want to actually grok the data from a command as it is running.</p>\n'}, {'answer_id': 78481, 'author': 'Mark Hattarki', 'author_id': 14424, 'author_profile': 'https://Stackoverflow.com/users/14424', 'pm_score': -1, 'selected': False, 'text': '<p>It just dawned on me that you are probably looking for one of the popen functions.</p>\n\n<p>from: <a href="http://docs.python.org/lib/module-popen2.html" rel="nofollow noreferrer">http://docs.python.org/lib/module-popen2.html</a></p>\n\n<p>popen3(cmd[, bufsize[, mode]])\n    Executes cmd as a sub-process. Returns the file objects (child_stdout, child_stdin, child_stderr). </p>\n\n<p>namaste,\nMark</p>\n'}, {'answer_id': 78482, 'author': 'Charles Duffy', 'author_id': 14122, 'author_profile': 'https://Stackoverflow.com/users/14122', 'pm_score': 4, 'selected': True, 'text': "<p>It can be done. As of Python 2.5, however, this mechanism is Linux-specific and not portable:</p>\n\n<pre><code>import subprocess\nimport sys\n\nfile = sys.argv[1]\np1 = subprocess.Popen(['hg', 'cat', file], stdout=subprocess.PIPE)\np2 = subprocess.Popen([\n    'gvimdiff',\n    '/proc/self/fd/%s' % p1.stdout.fileno(),\n    file])\np2.wait()\n</code></pre>\n\n<p>That said, in the specific case of diff, you can simply take one of the files from stdin, and remove the need to use the bash-alike functionality in question:</p>\n\n<pre><code>file = sys.argv[1]\np1 = subprocess.Popen(['hg', 'cat', file], stdout=subprocess.PIPE)\np2 = subprocess.Popen(['diff', '-', file], stdin=p1.stdout)\ndiff_text = p2.communicate()[0]\n</code></pre>\n"}, {'answer_id': 78923, 'author': 'pjz', 'author_id': 8002, 'author_profile': 'https://Stackoverflow.com/users/8002', 'pm_score': 2, 'selected': False, 'text': '<p>This is actually an example in the <a href="https://docs.python.org/2.4/lib/node242.html" rel="nofollow noreferrer">docs</a>:</p>\n\n<pre><code>p1 = Popen(["dmesg"], stdout=PIPE)\np2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)\noutput = p2.communicate()[0]\n</code></pre>\n\n<p>which means for you:</p>\n\n<pre><code>import subprocess\nimport sys\n\nfile = sys.argv[1]\np1 = Popen(["hg", "cat", file], stdout=PIPE)\np2 = Popen(["gvimdiff", "file.txt"], stdin=p1.stdout, stdout=PIPE)\noutput = p2.communicate()[0]\n</code></pre>\n\n<p>This removes the use of the linux-specific /proc/self/fd bits, making it probably work on other unices like Solaris and the BSDs (including MacOS) and maybe even work on Windows.</p>\n'}]
['https://Stackoverflow.com/questions/78431', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12650/']