Start nano as a subprocess from python, capture input

I am trying to run a text editor (nano) from within Python, ask the user to enter text, and then capture the text after they are written (Control-O). I have not worked with the module subprocessbefore, nor pipes, so I do not know what to try next.

So far I have this code:

a = subprocess.Popen('nano', stdout=subprocess.PIPE, shell=True)

Where ashould I capture the conclusion. This code, however, does not call nano, but instead makes the python terminal behave strangely. The Up and Down keys (history) stop working, and the Backspace key becomes dysfunctional.

Can someone point me in the right direction to solve this problem? I understand that I may have to read on pipes in Python, but the only information I can find is the module pipes, and that doesn't help much.

+3
source share
2 answers

Control-O in Nano writes to an editable file , i.e. not to standard conclusions - therefore, before trying to capture stdout and just read the file after it is written by the user, it exits Nano. For example, 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 press control-O Return control-X and:

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


>>> 
+6
source

, , nano. , -.

, ( , ), , - . , , , . , .

, os.system. , nano escape- (, curses), . , .

, $EDITOR, , nano. , .

+3

All Articles