[CentralOH] Filenames For Pipes?

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Tue Aug 21 03:44:35 CEST 2012

How can I do the following from within Python, without invoking 
bash and without creating intermediate files? How can I get a 
filename for a pipe or handle?

   diff <(gunzip <old.gz) <(gunzip <new.gz)

I know how to do gunzip <old.gz in a subprocess with Popen, and 
route its stdout to a PIPE into another subprocess' stdin, 
but I don't know how to get a file name for that stdout to give 
as a command line argument to diff. 

The following code samples either don't work, or are undesirable. 

The following does not work because arguments must be file names. 

   diff_file = subprocess.Popen(
       ['diff', '<(gunzip <old.gz)', '<(gunzip <new.gz)'], 

The following works by invoking bash, but I'd rather avoid invoking bash. 

   from subprocess import Popen, PIPE
   p1 = Popen(
       ['bash', '-c', 'diff <(gunzip <old.gz) <(gunzip <new.gz)'], stdout=PIPE)
   for line in p1.stdout:
       print line.rstrip('\r\n')

More information about the CentralOH mailing list