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
Withering soul
Little by little she dies everyday
Demons everywhere she's losing her sight
Pain has reached her bones
Breathing freely is a luxury now
she resists but she knows she is a withering soul
she is screaming but can anyone hear her?
The world's gone deaf
All eyes are shut, no one sees what's right in front of them
She is not the only one
She won't be the last one
Time is a flat circle, Nietzsche said
Once upon a time humans came into this world, cursed to repeat their doings time and time and time again
Fading souls don't await the eternal rest
There is no rest, there is no end to their agony
Trapped they are, born prisoners
Freedom was never a dream only an illusion
#review
#modern#poetry
🌈SOLD❤️ for $10,000,000. (CAN) - Located in West Vancouver Canada.
#Luxury#Modern#Mansion
📷Listed By malcolmhasman
💁Via @TheLuxuryClub
✔️🔜@R_Group_Official
━━━━━━━━━━━━━━━━━