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
🐍During this festive season of the #LunarNewYear, the entire CyberCharge team wishes you a Happy Year of the Snake!
May this year bring prosperity and joy to you and your loved ones 🫂
Thank you for your continued support, helping us grow and achieve incredible milestones.
We're just getting started! 🚀
🤖Earn NOW📱X(Twitter) 📱Chat💬Discord📱YouTube
Dear friends,
Warm greetings on the occasion of #LunarNewYear!
We wish you good health, happiness and prosperity! May this upcoming year bring along success and great fortune for you and your families.
Sincerely yours,
Russian Mission to #ASEAN
Bringing good luck and prosperity into the new year with the power of red! From lanterns 🏮 to envelopes 🧧, red is a traditional color of #ChineseNewYear. Don't forget to add a touch of red to your celebrations for a prosperous.
Принося удачу и процветание в новый год силой красного! От фонарей до конвертов, красный является традиционным цветом #ChineseNewYear. Не забудьте добавить нотку красного к своим празднованиям для процветающего.
#LunarNewYear#wallpaper
🧧 Follow 🧧@PeachLovesGoma🧧
Wish everyone a happy Lunar New Year! Bring glory back to Hong Kong in the year of dragon!
#HKDMovement
#glorytoHongKong
#lunarnewyear
#standwithhongkong