3 ms·
Actually I have recently analyzed some python code that does some shell calls and if I can eliminate them with pure python. It was quite frustrating. A lot of
by hannob 7y ago
Actually I have recently analyzed some python code that does some shell calls and if I can eliminate them with pure python.
It was quite frustrating. A lot of stuff that the shell has simple functionality for can't be done easily in python.
Examples:
* Call to chown -R. Sure, python can change file owners (os.chown()). But it has no parameter to do this recursively. You need to build a loop around it over some file iteration function.
* Call to setfacl. Can't be done with the python standard library, needs pylibacl and you can't rely on it being installed.
* Call to killall -HUP [processname]. You can send kill signals with python, but only to pids, not to process names. You'd have to parse the system process list or something like that in order to simulate.
In all cases I decided to keep the shell call, it just wasn't worth it converting that to less readable python code.
- ofrzeta 7y agoFair enough, choosing the right tool for the job. On my own servers I can take care that the requires modules are installed and even use virtual envs for stable library versioning. This is how we do Let's Encrypt renewal because I can use a Python library for Route53 authentication that works. Your first example could be solved with something like [os.chown(p, uid, gid) for p in pathlib.Path('.').rglob('*')] Obviously this is more verbose than chown but I think it's ok in a script.