[CentralOH] Filenames For Pipes?

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Thu Aug 23 03:17:23 CEST 2012


On Wed, 22 Aug 2012 19:07:29 -0400, William McVey <wam at cisco.com> wrote:

> On Wed, 2012-08-22 at 18:39 -0400, jep200404 at columbus.rr.com wrote:
> > I'm using the following smelly code: 
> >     fd_name = '/dev/fd/%d' % file.fileno()
> > There have to be better ways. What are they? 
> 
> I don't exactly understand what you're trying to do in terms of your
> python program. 

I'm using external programs from within Python. 

> The example you gave at the start of the thread showed
> the bash syntax for dealing with open descriptors as filenames. This
> makes sense for bash, which has to be able to convert its file handle
> redirection to binaries which generally prefer to handle filenames. But
> in python, operations are rarely done with either file descriptors or
> even filenames (beyond initial program setup) and it is much more common
> to process arbitrary iterators (or at least things that behave like file
> objects). 

Yup. I'm dealing with the mismatch between the bash way of 
doing things and the Pythonic way of doing things. 
Here's some simplified code to show what I'm playing with. 

[jep200404 at test ~]$ ll | gzip >old.gz
[jep200404 at test ~]$ ll | gzip >new.gz
[jep200404 at test ~]$ diff <(gunzip <old.gz) <(gunzip <new.gz)
1c1
< total 536336
---
> total 536340
16c16,17
< -rw-rw-r--.  1 jep200404 jep200404         0 Aug 23 01:00 old.gz
---
> -rw-rw-r--.  1 jep200404 jep200404         0 Aug 23 01:00 new.gz
> -rw-rw-r--.  1 jep200404 jep200404       570 Aug 23 01:00 old.gz
[jep200404 at test ~]$ python
Python 2.6.6 (r266:84292, Jun 18 2012, 14:18:47) 
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import Popen, PIPE
>>> 
>>> old_file = Popen(["gunzip"], stdin=open('old.gz', 'rb'), stdout=PIPE).stdout
>>> new_file = Popen(["gunzip"], stdin=open('new.gz', 'rb'), stdout=PIPE).stdout
>>> 
>>> old_fd_name = '/dev/fd/%d' % old_file.fileno()
>>> new_fd_name = '/dev/fd/%d' % new_file.fileno()
>>> 
>>> with (Popen(
...         ['diff', old_fd_name, new_fd_name],
...         stdout=PIPE).stdout
...         ) as diff_stdout:
...     for line in diff_stdout:
...         # process line
...         print line,
... 
1c1
< total 536336
---
> total 536340
16c16,17
< -rw-rw-r--.  1 jep200404 jep200404         0 Aug 23 01:00 old.gz
---
> -rw-rw-r--.  1 jep200404 jep200404         0 Aug 23 01:00 new.gz
> -rw-rw-r--.  1 jep200404 jep200404       570 Aug 23 01:00 old.gz
>>> 
[jep200404 at test ~]$



More information about the CentralOH mailing list