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?
Instead of doing some bash black magic, this could be accomplished pretty easily, and this is a working solution
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