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
A very happy International Women's Day to all from the Russian Foreign Ministry!
Dear women, colleagues, loved ones and family — we wish you all the best ❤️
May you enjoy happiness and good health 💐
#IWD2024
Happy #InternationalWomensDay
We recognise all women actively contributing to the strengthening of Nigeria's health security!
We appreciate all women whose resilience and expertise drive positive change and accelerate progress in making our communities healthier, stronger, and more vibrant.
#IWD2024
📌 Did you know that you can use the IRAS Relief Checker to find out more about the tax reliefs and/or rebates you may be qualified for?
Meet working mum Boon Hui, who is no stranger to the hectic, fast-paced life of the media industry. She’s thankful that despite the demanding schedule, her parents can help manage her teenage boys while she’s at work.
She’s also appreciative of the tax reliefs and rebates which have considerably lightened the family’s financial load.
This #internationalwomensday, we’d like to honour the strong women, families, friends and allies in our lives – for the seen and unseen labour they’ve carried at home and at work to help make our lives a little lighter. ❤
#iwd2024
🙋🏻♀️ Huquqlari himoyalangan va imkoniyatlarga ega ayollar O‘zbekiston🇺🇿 farovonligining asosidir!
8️⃣ Xalqaro xotin-qizlar bayrami muborak bo'lsin!
#WomensDay#IWD2024#8mart
🎉 Xalqaro xotin-qizlar kuniga tayyormisiz?
🤔 Qani, erkak kishini ilhomlantirib, hayoti va ishini yaxshilashga qodir ayol kimligini bilaylikchi!
✅ Bugun savolimizga Diyor Mirzaahmedov, O'zbekiston marketing assotsiatsiyasi raisining javobini e’tiboringizga taqdim etamiz.
—
#WomensDay#InspireInclusion#InvestInWomen#IWD2024