annoying stdin/stdout + pipes problem

per9000 per9000 at gmail.com
Sun Sep 23 09:40:21 EDT 2007


Hi,
I want to create a program that reads input from stdio that can prompt
a user for input while doing so without getting into problems.

A simplified version of what I want:

I have an input file
***>cat input.txt
foo bar baz
fubar barooba xyxxyt
raboof txet
black knight

And a file that replaces certain words
***>cat replace2.py
from sys import stdin, stdout

def censor(foo, bar, input):
    return input.replace(foo, bar)

# i = raw_input('Remove what? ').strip()
# o = raw_input('Replace "%s" with what? ' %i).strip()

for line in stdin.xreadlines():
    line = censor('foo', 'candy', line)
    line = censor('bar', 'donkey', line)
    line = censor('baz', 'hare rama', line)
    # line = censor(i, o, line)
    stdout.write(line)

***>cat input.txt | python replace2.py
candy donkey hare rama
fudonkey donkeyooba xyxxyt
raboof txet
black knight

As you can see I have commented out what I'd like to do but do not
know how to. Can I somehow halt the previous print to stdout (cat
input.txt) - if so can I then start it again, and how?

I want to use this to use it in a small encryption program called txet
in order to do something like this:
tar -blabla ~ | gzip -blabla | txet -e -stdin -stdout > /tmp/
home.tar.gz.txet
Of course I want to use it to get the password from the user.

Thanks,
Per

PS: a prototype can be downloaded from www.txet.org




More information about the Python-list mailing list