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
Upgrade your skills for free:
🧑🎓Master Course : Making the Business Case for Sustainability
▪️▪️▪️▪️▪️▪️▪️▪️
🧑🎓Teaching Methods, Teaching Quality & Teaching Delivery 3.0
▪️▪️▪️▪️▪️▪️▪️▪️
⚠️ Limited time offers
🧑🎓🧑🎓🧑🎓
https://SkillDiscounts.com/
@SkillDiscounts
#Learning_for_free
#FreeCoupon
#FreeLearning
Upgrade your skills for free:
🧑🎓Master Course in Smart Cities, Urban Planning & Development
▪️▪️▪️▪️▪️▪️▪️▪️
⚠️ Limited time offers
🧑🎓🧑🎓🧑🎓
https://SkillDiscounts.com/
@SkillDiscounts
#Learning_for_free
#FreeCoupon
#FreeLearning
Upgrade your skills for free:
🧑🎓Master Course in Corporate Social Responsibility (CSR)
▪️▪️▪️▪️▪️▪️▪️▪️
⚠️ Limited time offers
🧑🎓🧑🎓🧑🎓
https://SkillDiscounts.com/
@SkillDiscounts
#Learning_for_free
#FreeCoupon
#FreeLearning
Upgrade your skills for free:
🧑🎓Master Course in Climate Change Impact on Business
▪️▪️▪️▪️▪️▪️▪️▪️
🧑🎓Multigenerational Leadership & Reskilling Your Organization
▪️▪️▪️▪️▪️▪️▪️▪️
🧑🎓Master Course in Project Management 2.0
▪️▪️▪️▪️▪️▪️▪️▪️
⚠️ Limited time offers
🧑🎓🧑🎓🧑🎓
https://SkillDiscounts.com/
@SkillDiscounts
#Learning_for_free
#FreeCoupon
#FreeLearning
Upgrade your skills for free:
🧑🎓AI-Powered Change Management for Digital Transformation
▪️▪️▪️▪️▪️▪️▪️▪️
⚠️ Limited time offers
🧑🎓🧑🎓🧑🎓
https://SkillDiscounts.com/
@SkillDiscounts
#Learning_for_free
#FreeCoupon
#FreeLearning
Upgrade your skills for free:
🧑🎓Curso Completo de Mikrotik. Configuraciones y mucha Práctica
▪️▪️▪️▪️▪️▪️▪️▪️
🧑🎓Programador Java en 37 Días Con práctica de Entrevista y más
▪️▪️▪️▪️▪️▪️▪️▪️
🧑🎓NumPy, SciPy, Matplotlib & Pandas A-Z: Machine Learning
▪️▪️▪️▪️▪️▪️▪️▪️
⚠️ Limited time offers
🧑🎓🧑🎓🧑🎓
https://SkillDiscounts.com/
@SkillDiscounts
#Learning_for_free
#FreeCoupon
#FreeLearning
Upgrade your skills for free:
🧑🎓Carbon Capture (CCUS): Governance, Policy, & Social License
▪️▪️▪️▪️▪️▪️▪️▪️
🧑🎓The Complete JavaScript Course: From Zero to Expert
▪️▪️▪️▪️▪️▪️▪️▪️
🧑🎓Certificado Profissional em Liderança e Gestão
▪️▪️▪️▪️▪️▪️▪️▪️
⚠️ Limited time offers
🧑🎓🧑🎓🧑🎓
https://SkillDiscounts.com/
@SkillDiscounts
#Learning_for_free
#FreeCoupon
#FreeLearning
Upgrade your skills for free:
🧑🎓Mastering Manus: AI Automation for Marketing & Content
▪️▪️▪️▪️▪️▪️▪️▪️
🧑🎓AI for Business Analysts
▪️▪️▪️▪️▪️▪️▪️▪️
🧑🎓Entrepreneurial Guide to AI Strategy
▪️▪️▪️▪️▪️▪️▪️▪️
⚠️ Limited time offers
🧑🎓🧑🎓🧑🎓
https://SkillDiscounts.com/
@SkillDiscounts
#Learning_for_free
#FreeCoupon
#FreeLearning
Upgrade your skills for free:
🧑🎓Emotional Intelligence in Teams and Collaboration ( EQ )
▪️▪️▪️▪️▪️▪️▪️▪️
🧑🎓Data Visualization: Advanced Storytelling for Sales Success
▪️▪️▪️▪️▪️▪️▪️▪️
🧑🎓AI-Driven Business Process Modeling
▪️▪️▪️▪️▪️▪️▪️▪️
⚠️ Limited time offers
🧑🎓🧑🎓🧑🎓
https://SkillDiscounts.com/
@SkillDiscounts
#Learning_for_free
#FreeCoupon
#FreeLearning