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
🌍Urho Kekkonen National Park, Finland
📏
📏
📏
📏
📏
📏
📏
📏
⭐ Urho Kekkonen National Park offers breathtaking wilderness, with pristine forests, peaceful lakes, and an untouched natural landscape.
📏
📏
📏
📏
📏
📏
📏
📏
#Finland🇫🇮@voyage
🎅Santa Claus Village👋
📏
📏
📏
📏
📏
📏
📏
📏
🟠According to tradition, Santa Claus was born in Lapland. His village is considered his official home, making it one of Finland’s most visited tourist attractions.
📏
📏
📏
📏
📏
📏
📏
📏
#Finland🇫🇮@voyage
📍Finland🇫🇮
Finland is known as the "land of a thousand lakes," celebrated for its rich natural beauty, untouched lake landscapes, and pristine forests. 🌲🏞️
The homeland of the Moomin trolls and Santa Claus, the land of a thousand lakes, the land of the midnight sun – this is how Finland is commonly described. 🎅🌅
The Finns themselves call their country Suomi, which means "land of the swamps." 🌿
#Finland
@voyage
Aalto University Summer Program 2026 in Finland 🇫🇮 (Fully Funded) 🇪🇺
Visit: https://opportunitiescorners.com/aalto-university-international-summer-program/
Open to all Nationals. No Application Fee.
The Program Covers Airfare, Stipend, Accommodation, Food.
Deadline: 31st Jan 2026
#Finland#OpportunitiesCorners