annoying stdin/stdout + pipes problem

P

per9000

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
 
D

Damjan

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. ....
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?

The trick (which works on Linux for sure) is to open /dev/tty and ask
question/get input on/from it.

fp = open('/dev/tty', 'r+')
fp.write('Prompt: ')
answer = fp.readline().strip()


I'm not usre for other *nix-es, and on Windows this will surelly not work
 
P

per9000

...

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

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
 
G

Gabriel Genellina

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?

On Windows it's almost the same, using "conin$" as the filename for
console input, and "conout$" as the filename for console output.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top