Python + bash
Если вам часто требуется запускать shell команды из Python-кода, какой способ вы используете?
Самый низкоуровневый это функция os.system(), либо os.popen(). Рекомендованный способ это subprocess.call(). Но это всё еще достаточно неудобно.
Советую обратить своё внимание на очень крутую библиотеку sh.
Что она умеет?
🔸 удобный синтаксис вызова команд как функций
# os
import os
os.system("tar cvf demo.tar ~/")
# subprocess
import subprocess
subprocess.call(['tar', 'cvf', 'demo.tar', '~/'])
# sh
import sh
sh.tar('cvf', 'demo.tar', "~/")
🔸 простое создание функции-алиаса для длинной команды
fn = sh.lsof.bake('-i', '-P', '-n')
output = sh.grep(fn(), 'LISTEN')
в этом примере также задействован пайпинг
🔸 удобный вызов команд от sudo
with sh.contrib.sudo:
print(ls("/root"))
Такой запрос спросит пароль. Чтобы это работало нужно соответствующим способом настроить юзера.
А вот вариант с вводом пароля через код.
password = "secret"
sudo = sh.sudo.bake("-S", _in=password+"\n")
print(sudo.ls("/root"))
Это не все фишки. Больше интересных примеров смотрите в документации.
Специально для Windows💀 юзеров
#libs#linux
R✨e✨m✨e✨m✨b✨e✨r
Never forget
- Good luck, Have Fun, Don't Die.. 2026 Film putting it right in our faces. Real eyes Realize Real Lies. ..
,•°°•ꨄ•°°•,
✨🌞✨
’•✧•’
( Big Homie )¯\_(ツ)_/¯
All the world is their stage. 🎬
𝐄 𝐍 𝐉 𝐎 𝐘 | 🌟🌟🌿
#Wisdom
It's not what we bought but what we built
It's not what we got but what we shared
It's not our competence but our characters
And it's not our success but our significance
Live a life that matters. Live a life of love.
Unknown
#wisdom
@quietworld🍃
The day the child realizes that all adults are imperfect he becomes an adolescent; the day he forgives them, he becomes an adult; the day he forgives himself he becomes wise.
– Alden N –
#wisdom
@quietworld🍃
We judge people by what they see and hear, but we do not see the qualities that are hidden in their hearts. Avoid suspicion. Let's not say bad things about each other. Let us not gossip or insult.
#wisdom
We can control the words we say, but we can't control what people understand.
We can control what we say to people, but we can't control what other people say to others about us.
That's normal and just a part of life so be it. I'm at peace when I control only what I can control and refuse to take part in things that I cannot control especially those which do not matter at all. ☺️✨🦋
#thoughts#wisdom
@quietworld🍃
To live only for some future goal is shallow. It’s the sides of the mountain that sustain life, not the top.
– Robert M. Prisig
#life#wisdom
@quietworld🍃