0

I'd like to open an editor (cli or gui-based) via Python script. I can do this with os.system('vim file'), but I'd also like to get output in case command fails. subprocess.getstatusoutput('vim file') doesn't work for opening programs with specific interface.

What's the best alternative?

2
  • 2
    Not sure what you want to do, but you should really use os.environ['EDITOR'] and not start some editor you choose. Commented Nov 1, 2011 at 12:31
  • Don't worry, I'm not trying to force my grandma to use vi. I've written a little tools that parses a file containing list of editors and path aliases. E.g. I can do ed httpd and it will run vim -arg1 -arg2 /etc/httpd/conf/httpd.conf Commented Nov 1, 2011 at 15:31

2 Answers 2

2
subprocess.check_call([EDITOR, file_path])

will raise an OSError exception if EDITOR fails to run.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use os.execv(path, args) if the command fails, an OSError will be thrown. You can find more details on os.execv documentation.

2 Comments

But if it succeeds, os.execv will not return to the Python interpreter.
He would like to get output in case command fails, I think that would work. But I agree with you, if he wants to get ALL output from that execution, it would not work properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.