2

I'm using os.popen() to run terminal commands in multiple projects.

I keep reading that os.popen() is deprecated though and I should use Popen() instead, but I haven't been able to get Popen() to work for me (it freezes the pi for some reason so I can move the mouse but I can't click or type or open the terminal to shut down safely) and its more code and it seems like a lot of trouble to change code that works perfectly fine.

Is there a reason I should actually work harder to stop using os.popen() on my pi?

1 Answer 1

4

According to docs.python.org os.popen() is stacked on top of subprocess.Popen() - at least for Python 3.x:

This is implemented using subprocess.Popen; see that class’s documentation for more powerful ways to manage and communicate with subprocesses.

So other than losing some of the more finegrained controls the underlying subprocess.Popen() provides there is nothing fundamentally wrong with using os.popen(). Since it uses subprocess.Popen() I will assume that the safety & reliability issues reported for Python 2.x are not an issue here.

With Python 2.x however the use of os.popen() is not recommended.

Deprecated since version 2.6: This function is obsolete. Use the subprocess module. Check especially the Replacing Older Functions with the subprocess Module section.

See more: Python: when should I use subprocess.Popen instead of os.popen?

2
  • So if i'm using Python3 I should just not use it? Commented Aug 22, 2017 at 19:50
  • 1
    @StarSweeper to my understanding it is ok to use it with Python3.
    – Ghanima
    Commented Aug 22, 2017 at 19:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.