12 comments

[ 3.1 ms ] story [ 39.9 ms ] thread
“`os` is bad, don’t use it.” Ok, but why? What’s wrong with it?
This was my question. What’s wrong with using os.system to run commands?
This is a problem I have very frquently when writing python scripts on my local box. Sh module seems like exactly what I need, but does it work on wandows?
> sh relies on various Unix system calls and only works on Unix-like operating systems - Linux, macOS, BSDs etc. Specifically, Windows is not supported.

from: https://amoffat.github.io/sh/

I wish I could type the face I made when the alternative to import os is to install a package from pip. The most reliable Python is written only using builtins.
Maybe I value cross-platform too highly, but I don't see the use of this post... Am I missing something? The claim is that there should be only one way to do it in Python. But then goes on to describe a way that doesn't even work on a major platform.

I've worked on a project recently, a rough prototype, that executes a vast amount of commands via subprocess[1]. I've been very happy with the results of subprocess.popen(). It works well on Linux, Macs, and windows with minimal tweaking.

[1] https://docs.python.org/3/library/subprocess.html

> The claim is that there should be only one way to do it in Python.

Python developers and failing to adhere to this part of the Zen of Python. Name a more iconic duo.

I have seen this advice many times but I end up using os.system() 70% of the time. If subprocess and other methods were smart enough to support running something like " ls -l aS " without requiring additional parameters/handling I would use them. If I was shipping a user facing production app I would be using the more recommended apis as well. But when I just need a freaking command run and I don't care about the results and there is no dynamic parameter in the command why would I add all that code and complexity? Most of my use cases could also be done as a bash script except python is nicer/leaner so i prefer it instead.
I think the correct answer is "don't"
When I read a post like this, I don't mind the article being written, or votes by those who found it interesting. Everyone's learning. What gets me is that there's no way to not make it show highly ranked. Moderation via comments after posts rank up doesn't scale.