0

If I am taking over stdout, with an interactive Python script that launches a console GUI such as curses or urwid; after some actions and closing the loop, how could I cd to a path printed in stdout?

For example,

import sys
import urwid

def check_for_q(key):
    if key in ('q', 'Q'):
        raise urwid.ExitMainLoop()

txt = urwid.Text(u"Thanks for helping")
fill = urwid.Filler(txt, 'top')
loop = urwid.MainLoop(fill, unhandled_input=check_for_q)
loop.run()

sys.stdout.write('/usr/bin')

When run, and pressing q or Q to quit the urwid loop:

(py27) $ python curses_script.py

/usr/bin

(py27) $

If this was a simple python script that only printed to stdout, I could cd $(python simple_script.py). With the above, however, this will hang as the python subshell fails to hijack stdout and process input.

Is it possible to work around this without writing to file?

4

1 回答 1

0

而不是做一些 bash 黑魔法,这可以很容易地完成,这是一个可行的解决方案

temp_file=$(mktemp /tmp/curses_script.XXXXXX)
python curses_script.py temp_file
# write to temp file in script...
cd $(cat temp_file)
rm $temp_file
于 2016-10-30T18:08:08.637 回答