Start nano as a subprocess from python, capture input

前端 未结 2 1242
死守一世寂寞
死守一世寂寞 2021-01-21 05:14

I\'m trying to start a text editor (nano) from inside Python, have the user enter text, and then capture the text once they writeout (Control-O). I haven\'t worked with the

2条回答
  •  终归单人心
    2021-01-21 05:43

    Control-O in Nano writes to the file being edited, i.e., not to standard output -- so, forego the attempt to capture stdout and just read the file once the user writes it out and exits Nano. E.g., on my Mac:

    >>> import tempfile
    >>> f = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
    >>> n = f.name
    >>> f.close()
    >>> import subprocess
    >>> subprocess.call(['nano', n])
    

    Here, I write "Hello world!" then hit control-O Return control-X , and:

    0
    >>> with open(n) as f: print f.read()
    ... 
    Hello world!
    
    
    >>> 
    

提交回复
热议问题