[CentralOH] Filenames For Pipes?

William McVey wam at cisco.com
Thu Aug 23 17:36:09 CEST 2012


On Wed, 2012-08-22 at 21:17 -0400, jep200404 at columbus.rr.com wrote:
> I'm using external programs from within Python. 
<snip>

Seems like the easiest thing to do is just call zdiff against your
gzipped files directly and process that program's output stream. If
that's not to your liking, I'd suggest skipping the external programs
altogether and use the python solutions to this problem, namely the gzip
and difflib libraries:

        >>> import gzip, difflib, pprint
        >>> a = gzip.GzipFile("a.gz")
        >>> b = gzip.GzipFile("b.gz")
        >>> pprint.pprint(list(
        ...   difflib.unified_diff(a.readlines(), b.readlines(),
        ...                        fromfile="gzipped a", tofile="gzipped
        b")))
        ['--- gzipped a\n',
         '+++ gzipped b\n',
         '@@ -1,4 +1,5 @@\n',
         '-total 8\n',
         '+total 12\n',
         ' drwxr-xr-x  2 wam  wam  4096 Aug 23 10:58 .\n',
         ' drwxrwxrwt 18 root root 4096 Aug 23 10:58 ..\n',
         '--rw-r--r--  1 wam  wam     0 Aug 23 10:58 a.gz\n',
         '+-rw-r--r--  1 wam  wam   105 Aug 23 10:58 a.gz\n',
         '+-rw-r--r--  1 wam  wam     0 Aug 23 10:58 b.gz\n']

unified_diff() returns a generator, so I convert it to a list before
pushing it to pretty pprint.

If that's not to your liking, then I'd probably recommend using named
pipes created via os.mkfifo() in a tempfile.mkdtemp() created
directory. 

  -- William



More information about the CentralOH mailing list