i am trying to recieve input from external text editor using python's subprocess but I am not sure why i am getting the error

diff = subprocess.Popen('open(os.path.join(os.getcwd(), "foo.txt") "w")', stdout=subprocess.PIPE)
pr = subprocess.Popen(sublime, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, stdin=diff.stdout)
pr.wait()
if pr.returncode == 0:
    msg = pr.stdout.read()
    print msg
    er = pr.stderr.read()
    print er
    fo.close()




    diff = subprocess.Popen('open(os.path.join(os.getcwd(), "foo.txt") "w")', stdout=subprocess.PIPE)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1308, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

any help would be appreciated..

I haven't used the subprocess module before, but why do you try to write to the file you try to read from?

I think you are trying to do something completely impossible. The first argument in Popen() must be a valid command line (either a string or a list, see the doc) which you could write in a terminal and execute. You can't write

open(os.path.join(os.getcwd(), "foo.txt") "w")

in a terminal and expect a pleasant answer from your computer.

If you want to read from an editor window in your python program, the normal way to go is to use one of the GUI frameworks.

ok I got it working somewhat, but I can only have sublime to be able to use as editor

filePath = os.path.join(os.getcwd(), "foo.txt")


fo = open(filePath, "w")
pr = subprocess.Popen(editor, stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)
data = open(filePath, 'r').read()
out, err = pr.communicate(data)
print out

With TextEdit it hangs, and and with nano it gives this error: Received SIGHUP or SIGTERM and with vim I get this error Vim: Warning: Output is not to a terminal Vim: Warning: Input is not from a terminal and terminal gets stuck while I want to be able to use any editor.

Edited 2 Years Ago by krystosan: missed code

I don't see how you can receive input from an external editor using subprocess.Popen() since the external editor does not usually send user input to the outside world (via stdout or a socket, ...).

An exception to this is when the editor saves the file being edited. This changes the modification time of a file on disk, and this event can be intercepted by modules watching files or directory (such as pyinotify or perhaps QtCore.QFileSystemWatcher). Your program could then read the file on disk.

Another way is to see if there is a plugin for your editor which can send the contents of the editor window to a socket or a pipe.

what are you talking man, the code I posted above works for sublime text

i can get it to work with sublime but instead of sublime can you test with other editors like vim or nano or notepad?

This question has already been answered. Start a new discussion instead.