annoying stdin/stdout + pipes problem

per9000 per9000 at gmail.com
Mon Sep 24 03:04:07 EDT 2007


On 23 Sep, 18:24, Damjan <gdam... at gmail.com> wrote:
> > 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.
> ...
>
> The trick (which works on Linux for sure) is to open /dev/tty and ask
> question/get input on/from it.
> ...
>
> I'm not usre for other *nix-es, and on Windows this will surelly not work
> --
> damjan

Thanks,

it worked just fine on my Gnu/Linux machine, but I'd really like it to
be independent of OS. Any one else got some idea - or a windows
version of the same?

/Per

--

PS: for the record:
***>cat replace3.py && echo "---" && cat input.txt | python
replace3.py

from sys import stdin, stdout

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

fp = open('/dev/tty', 'r+')
fp.write('Remove what? ')
i = fp.readline().strip()
fp.write('Replace %s with what? ' %i)
o = fp.readline().strip()
fp.close()

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)
---
Remove what? black
Replace black with what? beige
candy donkey hare rama
fudonkey donkeyooba xyxxyt
raboof txet
beige knight





More information about the Python-list mailing list